Advertisement
dereksir

Untitled

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