Advertisement
dereksir

Untitled

Dec 21st, 2023 (edited)
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.81 KB | None | 0 0
  1. //..
  2.  
  3.     // Create an HTML tokenizer
  4.     z := html.NewTokenizer(resp.Body)
  5.  
  6.     // Loop through HTML tokens
  7.     for {
  8.         tokenType := z.Next()
  9.  
  10.         switch tokenType {
  11.         case html.StartTagToken, html.SelfClosingTagToken:
  12.             token := z.Token()
  13.  
  14.             // Check for all <li> element
  15.             if token.Data == "li" {
  16.                 // Process the details of the Pokémon within this <li> element
  17.                 processPokemonDetails(tokenizer)
  18.  
  19.                 // Exit the loop after processing the details
  20.                 return
  21.                 }
  22.             }
  23.  
  24.         if tokenType == html.ErrorToken {
  25.             break
  26.         }
  27.     }
  28. }
  29.  
  30. func processPokemonDetails(z *html.Tokenizer) {
  31.     // Parse Tokens for Relevant Data within the <li> element
  32.     for {
  33.         tokenType := z.Next()
  34.  
  35.         switch tokenType {
  36.         case html.StartTagToken, html.SelfClosingTagToken:
  37.             token := z.Token()
  38.  
  39.             // Parse Tokens for Relevant Data
  40.             switch token.Data {
  41.             case "h2":
  42.                 // Extracting Pokémon name
  43.                 tokenType = z.Next()
  44.                 if tokenType == html.TextToken {
  45.                     name := z.Token().Data
  46.                     fmt.Println("Name:", name)
  47.                 }
  48.  
  49.             case "span":
  50.                 // Check for the span with class "price"
  51.                 hasPriceClass := false
  52.                 for _, attr := range token.Attr {
  53.                     if attr.Key == "class" && strings.Contains(attr.Val, "price") {
  54.                         hasPriceClass = true
  55.                         break
  56.                     }
  57.                 }
  58.  
  59.                 // If the span has class "price," fetch the next token
  60.                 if hasPriceClass {
  61.                     tokenType = z.Next()
  62.  
  63.                     // Check if the next token is a span with class "amount"
  64.                     if tokenType == html.StartTagToken || tokenType == html.SelfClosingTagToken {
  65.                         nextToken := z.Token()
  66.                         if nextToken.Data == "span" {
  67.                             amountClass := false
  68.                             for _, attr := range nextToken.Attr {
  69.                                 if attr.Key == "class" && strings.Contains(attr.Val, "amount") {
  70.                                     amountClass = true
  71.                                     break
  72.                                 }
  73.                             }
  74.  
  75.                             // If the next span has class "amount," loop through and print its text content
  76.                             if amountClass {
  77.  
  78.                                 var currencySymbol, priceValue string
  79.  
  80.                                 for {
  81.                                     tokenType = z.Next()
  82.                                     if tokenType == html.TextToken {
  83.                                         currencySymbol = z.Token().Data
  84.  
  85.                                     }
  86.  
  87.                                     tokenType = z.Next()
  88.                                     if tokenType == html.TextToken {
  89.                                         priceValue = z.Token().Data
  90.  
  91.                                     } else if tokenType == html.EndTagToken && tokenizer.Token().Data == "span" {
  92.                                         break
  93.                                     }
  94.  
  95.                                     amount := currencySymbol + priceValue
  96.                                     fmt.Println("Price:", amount)
  97.                                 }
  98.                             }
  99.                         }
  100.                     }
  101.                 }
  102.  
  103.             case "img":
  104.                 // Extracting Pokémon image URL
  105.                 for _, attr := range token.Attr {
  106.                     if attr.Key == "src" {
  107.                         imageURL := attr.Val
  108.                         fmt.Println("Image URL:", imageURL)
  109.                     }
  110.                 }
  111.             }
  112.         }
  113.         if tokenType == html.ErrorToken {
  114.             break
  115.         }
  116.     }
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement