解决 go-colly 中的 noscript 问题
时间:2024-02-29
浏览:327
作者:佚名
小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《解决 go-colly 中的 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>解决方案
页面上将有一些 JavaScript 来更新此内容(<noscript> 部分适用于没有 JavaScript 的浏览器)。当您“查看源代码”时,您会看到服务器提供的原始 HTML;通过“检查元素”,您可以看到 DOM 的现状(即在更新此部分的任何脚本运行之后)。
Go-Colly 不运行 JavaScript,因此您需要另一种方法。选项包括查看 JavaScript 以了解它如何定位图像或使用 chromedp 等内容代替 go-colly。
理论要掌握,实操不能落!以上关于《解决 go-colly 中的 noscript 问题》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
