所以我正在从网站上制作一个抓取脚本。当抓取文本成功时,仅当抓取图像失败时。当我检查元素时,代码仍然正常,但是当我运行视图源时,图像包装代码更改为 noscript。所以我想是这样的,也许有人可以帮忙?
c.OnHTML(".postarea", func(h *colly.HTMLElement) {
as := Image{}
as.Name = h.ChildText(".headpost .entry-title")
h.ForEach(".maincontent", func(i int, x *colly.HTMLElement) {
ya := So{}
ya.Url = x.ChildAttr("#readerarea img", "src")
as.Image = append(as.Image, ya)
})
b, err := json.MarshalIndent(as, "", " ")
if err != nil {
log.Println("failed to serialize response:", err)
return
}
w.Header().Add("Content-Type", "application/json")
w.Write(b)
})
c.OnRequest(
这是示例 html 代码。
<div id="readerarea"><noscript>
<p><img loading="lazy"
src="#" alt=""
width="725" height="1024" class="alignnone size-full wp-image-72251" /><img loading="lazy"
src="#" alt=""
width="725" height="1024" class="alignnone size-full wp-image-72251" /><img loading="lazy"
src="#" alt=""
width="725" height="1024" class="alignnone size-full wp-image-72251" /><img loading="lazy"
src="#" alt=""
width="725" height="1024" class="alignnone size-full wp-image-72251" />
</p>
</noscript>
</div>