Advertisement
dereksir

Untitled

Dec 21st, 2023 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.94 KB | None | 0 0
  1. func main() {
  2.     //..http request
  3.  
  4.     // Use the html package to parse the response body from the request
  5.     doc, err := html.Parse(resp.Body)
  6.     if err != nil {
  7.         fmt.Println("Error:", err)
  8.         return
  9.     }
  10.  
  11.     // Define a function to traverse the HTML document
  12.     var processAllPokemon func(*html.Node)
  13.     processAllPokemon = func(n *html.Node) {
  14.         // Check for all <li> elements
  15.         if n.Type == html.ElementNode && n.Data == "li" {
  16.                     // Process the details of the Pokémon within this <li> element
  17.                     processPokemonDetails(n)
  18.         }
  19.  
  20.         // Traverse child nodes
  21.         for c := n.FirstChild; c != nil; c = c.NextSibling {
  22.             processAllPokemon(c)
  23.         }
  24.     }
  25.  
  26.     // Start processing the Pokémon details
  27.     processAllPokemon(doc)
  28. }
  29.  
  30. // Process the details of the Pokémon within the <li> element
  31. func processPokemonDetails(liNode *html.Node) {
  32.     // Define a function to traverse the HTML structure within the <li> element
  33.     var processNode func(*html.Node)
  34.     processNode = func(n *html.Node) {
  35.         switch n.Data {
  36.         case "h2":
  37.             // Extracting Pokémon name
  38.             if n.FirstChild != nil && n.FirstChild.Type == html.TextNode {
  39.                 pokemonName := n.FirstChild.Data
  40.                 fmt.Println("Name:", pokemonName)
  41.             }
  42.  
  43.         case "span":
  44.             // Check for the span with class "amount"
  45.             for _, a := range n.Attr {
  46.                 if a.Key == "class" && strings.Contains(a.Val, "amount") {
  47.                     // Print the text content of the "amount" span
  48.                     for c := n.FirstChild; c != nil; c = c.NextSibling {
  49.                         if c.Type == html.TextNode {
  50.                             fmt.Println("Price:", c.Data)
  51.                         }
  52.                     }
  53.                 }
  54.             }
  55.  
  56.         case "img":
  57.             // Extracting Pokémon image URL
  58.             for _, a := range n.Attr {
  59.                 if a.Key == "src" {
  60.                     pokemonImageURL := a.Val
  61.                     fmt.Println("Image URL:", pokemonImageURL)
  62.                 }
  63.             }
  64.         }
  65.  
  66.         // Traverse child nodes
  67.         for c := n.FirstChild; c != nil; c = c.NextSibling {
  68.             processNode(c)
  69.         }
  70.     }
  71.  
  72.     // Start processing the nodes within the <li> element
  73.     processNode(liNode)
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement