【问题标题】:Scrape ONLY a certain <div> using gocolly使用 gocolly 只刮取某个 <div>
【发布时间】:2021-01-30 18:48:18
【问题描述】:

我正在尝试使用 gocolly 制作网络爬虫。我只想在https://wol.jw.org/en/wol/h/r1/lp-e 上刮取ID 为dailyText&lt;div&gt; 元素。我该怎么做?

【问题讨论】:

  • 查看this example并将c.OnHTML("a[href]"...中的选择器替换为div#dailyText,然后相应地调整功能。如果不完全清楚,请随时提出更多问题或查看other examples

标签: go go-colly


【解决方案1】:

感谢 xarantolus 的回答。
这对我很有用(如果域允许我使用它,那就是。)

func main() {
    cly := colly.NewCollector(
        colly.AllowedDomains("https://yourpage.site"),
    )
    cly.OnHTML("body", func(e *colly.HTMLElement) {
        link := e.Attr("div")
        fmt.Printf("Link found: %q -> %s\n", e.Text, link)
        cly.Visit(e.Request.AbsoluteURL(link))
    })
    cly.OnRequest(func(r *colly.Request) {
        fmt.Println("Visiting", r.URL.String())
    })
    page := cly.Visit("https://yourpage.site")
    fmt.Print(page)
}

【讨论】:

    猜你喜欢
    • 2023-01-04
    • 2021-12-26
    • 2019-06-03
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多