My task is to find images urls inside an html
The problem
golang.org/x/net/htmlgithub.com/PuerkitoBio/goquery
The question
imgsrc=""
Notes:
//static2.ozone.ru/multimedia/spare_covers/1013531536.jpgGo getGo build
Parsed html page source
resp, _ := http.Get(url)
Code:
package main
import (
"golang.org/x/net/html"
"log"
"net/http"
)
func main() {
url := "http://www.ozon.ru/context/detail/id/34498204/"
if resp, err := http.Get(url); err == nil {
defer resp.Body.Close()
log.Println("Load page complete")
if resp != nil {
log.Println("Page response is NOT nil")
if document, err := html.Parse(resp.Body); err == nil {
var parser func(*html.Node)
parser = func(n *html.Node) {
if n.Type == html.ElementNode && n.Data == "img" {
var imgSrcUrl, imgDataOriginal string
for _, element := range n.Attr {
if element.Key == "src" {
imgSrcUrl = element.Val
}
if element.Key == "data-original" {
imgDataOriginal = element.Val
}
}
log.Println(imgSrcUrl, imgDataOriginal)
}
for c := n.FirstChild; c != nil; c = c.NextSibling {
parser(c)
}
}
parser(document)
} else {
log.Panicln("Parse html error", err)
}
} else {
log.Println("Page response IS nil")
}
}
}