我正在try 使用colly让特定的表循环其内容,但是表没有被识别,这是我到目前为止所得到的.

package main

import (
    "fmt"
    
    "github.com/gocolly/colly"
)

func main() {
    c := colly.NewCollector(
        colly.AllowedDomains("wikipedia.org", "en.wikipedia.org"),
    )
    
    links := make([]string, 0)

    c.OnHTML("div.mw-parser-output", func(e *colly.HTMLElement) {
        
        e.ForEach("table.wikitable.sortable.jquery-tablesorter > tbody > tr", func(_ int, elem *colly.HTMLElement) {
            fmt.Println(elem.ChildAttr("a[href]", "href"))
            links = append(links, elem.ChildAttr("a[href]", "href"))
        })
    })
    
    c.OnRequest(func(r *colly.Request) {
        fmt.Println("Visiting", r.URL.String())
    })

    c.Visit("https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population")
    fmt.Println("Found urls for", len(links), "countries.")
}

我需要循环考虑表中的所有tr元素.

推荐答案

原来类的名称实际上是wikitable.sortable,尽管在Chrome控制台中显示为wikitable sortable jquery-tablesorter.我不知道为什么名字会有这样的不同,但这解决了我的问题.

Go相关问答推荐

Term~T中的类型不能是类型参数,但可以是引用类型参数的切片

如何在使用中介资源时处理函数中的`defer`

GO错误:Tim.Time未实现driver.Valuer(缺少方法值)

租户GUID X的租户不存在self 邮箱帐户的租户(我是唯一的成员)

Golang使用Run()执行的命令没有返回

重新赋值变量时未清除动态类型-这是错误吗?

JWT 如何解析声明有效性和错误?

将这两个函数合二为一的惯用方法

调用库和直接操作效率有区别吗?

Go - 永远停止带有上下文的循环

在golang中以JSON格式获取xwwwformurlencoded请求的嵌套键值对

在 Go sync.Map 中,为什么这部分实现不一致或者我误解了什么?

将 firestoreinteger_value转换为整数

如何使用 go-git 将特定分支推送到远程

如何在模板中传递和访问 struct 片段和 struct

如何使用 Go 代理状态为 OK 的预检请求?

如何将文件上传到 Google Drive,并与使用服务帐户和 Golang 的任何人共享

gorm 获取列名

如何断言类型是指向golang中接口的指针

如何在程序退出时使用 golang 删除文件?