从网站go-colly中抓取描述信息。

huangapple go评论91阅读模式
英文:

Scrape discription from web site go-colly

问题

我尝试从img网站上爬取描述信息,但我不明白如何获取。以下是我的尝试:

pg := Program{}
slPG := []Program{}

c.OnHTML(".short", func(e *colly.HTMLElement) {
    pg.Name = e.ChildText("h2.short-cat")
    pg.Link = e.ChildAttr("a", "href")
    pg.Rating = e.ChildText("a.orating_res")
    pg.Discription = e.ChildText("div.short-text full-text video-box clearfix")

    slPG = append(slPG, pg)
})
for i := 1; i < 6; i++ {
    c.Visit("https://rsload.net/page/" + strconv.Itoa(i) + "/")
}

file, err := json.MarshalIndent(slPG, "", " ")
if err != nil {
    log.Println("无法创建'json'文件")
    return
}
_ = ioutil.WriteFile("files/Export.json", file, 0644)
英文:

I try scrape the description from website
img, but I not understand how to get there <br>
My trying

pg := Program{}
slPG := []Program{}

c.OnHTML(&quot;.short&quot;, func(e *colly.HTMLElement) {
	pg.Name = e.ChildText(&quot;h2.short-cat&quot;)
	pg.Link = e.ChildAttr(&quot;a&quot;, &quot;href&quot;)
	pg.Rating = e.ChildText(&quot;a.orating_res&quot;)
	pg.Discription = e.ChildText(&quot;div.short-text full-text video-box clearfix&quot;)

	slPG = append(slPG, pg)
})
for i := 1; i &lt; 6; i++ {
	c.Visit(&quot;https://rsload.net/page/&quot; + strconv.Itoa(i) + &quot;/&quot;)
}

file, err := json.MarshalIndent(slPG, &quot;&quot;, &quot; &quot;)
if err != nil {
	log.Println(&quot;Unable to create &#39;json&#39; file&quot;)
	return
}
_ = ioutil.WriteFile(&quot;files/Export.json&quot;, file, 0644)

答案1

得分: 1

请展示更多你的代码。具体来说,你需要展示你想要抓取的HTML部分。
我知道你有一部分文本和属性,但是colly库非常挑剔。如果你没有完全正确的信息,它将不会返回任何内容给你。
你是通过命令行中的fmt获取信息,还是将其解析成GUI,或者将其全部转换成文件?提供更多信息,我们可以提供帮助。

英文:

Please show more of your code. Specifically, you need to show the part of the html which you are trying to grab.
I know you have a part of the text and attributes, but the colly library is very picky. If you don't have exactly the right info. It will return nothing back to you.
Are you getting the information back via fmt in command line, or parsing it into a gui, or perhaps converting it all into a file? Provide more info and we can help.

huangapple
  • 本文由 发表于 2022年8月21日 17:06:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/73432983.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定