Advertisement
dereksir

Untitled

Dec 21st, 2023 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 1.11 KB | None | 0 0
  1. // process the details of the Pokémon within the <li> element
  2. func processNode(n *html.Node) {
  3.     switch n.Data {
  4.     case "h2":
  5.         // check if FirstChild node of the h2 element is a text
  6.         if n.FirstChild != nil && n.FirstChild.Type == html.TextNode {
  7.             // if yes, retrieve FirstChild's data (name)
  8.             name := n.FirstChild.Data
  9.             // print name
  10.             fmt.Println("Name:", name)
  11.         }
  12.  
  13.     case "span":
  14.         // check for the span with class "amount"
  15.         for _, a := range n.Attr {
  16.             if a.Key == "class" && strings.Contains(a.Val, "amount") {
  17.                 // retrieve the text content of the "amount" span
  18.                 for c := n.FirstChild; c != nil; c = c.NextSibling {
  19.                     if c.Type == html.TextNode {
  20.                         // print Pokemon price
  21.                         fmt.Println("Price:", c.Data)
  22.                     }
  23.                 }
  24.             }
  25.         }
  26.  
  27.     case "img":
  28.         // check for the src attribute in the img tag
  29.         for _, a := range n.Attr {
  30.             if a.Key == "src" {
  31.                 // retrieve src value
  32.                 ImageURL := a.Val
  33.                 // print image URL
  34.                 fmt.Println("Image URL:", ImageURL)
  35.             }
  36.         }
  37.     }
  38.  
  39.     // Traverse child nodes
  40.     for c := n.FirstChild; c != nil; c = c.NextSibling {
  41.         processNode(c)
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement