golang 没有名字参数_一日一技:在Golang下如何相对简单地开发爬虫?

0b8423b29a3d96ad4f21e271edbefe30.png

摄影:产品经理 墨鱼和鱿鱼做的香肠

我们说,语言决定思维方式。要掌握一种新的思维方式,学习一门新语言是最直接的方法。

我之前一直用 Python 来写爬虫,现在,尝试用 Golang 来实现一个简单的爬虫,请求网址,然后使用 XPath 提取数据。

这个爬虫项目使用 Go Mod 来管理依赖,执行下面的命令创建爬虫项目文件夹:

mkdir crawler_go
cd crawler_go
go mod init crawler_go

运行效果如下图所示:

a820417d1859c347534f1bedbc21975c.png

这3条命令会在crawler_go文件夹中创建一个 go.mod 的文件。然后,我们再创建一个爬虫文件crawler.go

6b602a26b683ad4155b01439b4215c77.png

我们知道,虽然 Python 自带了一个网络请求库urllib,但是我们一般会使用requests来请求网络。因为这个库才是Http for human。在 Golang 里面请求网络,也有一个库,叫做req[1]。我们在代码里面引入它,并获取一个网址。这次使用的是爬虫练习网站的地址:Chapter11_example_2[2]

编写如下代码:

package main

import (
    "fmt"

    "github.com/imroc/req"
)

func main() {
    url := "http://exercise.kingname.info/exercise_xpath_2.html"
    resp, _ := req.Get(url)
    fmt.Println(resp.String())
}

运行效果如下图所示:

6512b5949274b3289652593431bfec9f.png

使用 Go Mod 的好处,就在于当我们把一个新的第三方库添加到import语句后,第一次运行代码时,Go 会自动下载这个库。

需要说明一下,在代码第11行,resp, _:= req.Get(url),这里的下划线实际上是用来接收另外一个参数err,但是由于我不需要使用这个参数,所以可以使用下划线代替。

使用req库来请求网址,实际上也非常容易。那么接下来,我们想办法在 Golang 里面使用 XPath,从源代码中提取数据。这次用到的库叫做htmlquery[3]

相比在 Python 中使用 lxml,在 Golang 下面使用 htmlquery 多多少少还是要麻烦一些。我们来看看如何提取练习页面的标题:

package main

import (
    "fmt"
    "strings"

    "github.com/antchfx/htmlquery"
    "github.com/imroc/req"
)

func main() {
    url := "http://exercise.kingname.info/exercise_xpath_2.html"
    resp, _ := req.Get(url)
    selector, _ := htmlquery.Parse(strings.NewReader(resp.String()))
    title := htmlquery.FindOne(selector, "//title/text()")
    fmt.Println("标题为:", htmlquery.InnerText(title))
}

运行效果如下图所示:

f882444f2c7629c902e0671fd80ad551.png

从代码中可以看到,在 Golang 中使用htmlquery,每一次都要把节点传入 htmlquery 的某个函数里面。不能像 Python 里面一样通过链式调用对象的方法来获取数据。

现在我们来获取练习页上面的文字内容:

3180672f2474a65241de2fc9c1d44421.png

需要注意的是,第二条项目只有名字但是没有价格,我们需要在代码里面兼容这种情况:

package main

import (
    "fmt"
    "strings"

    "github.com/antchfx/htmlquery"
    "github.com/imroc/req"
)

func main() {
    url := "http://exercise.kingname.info/exercise_xpath_2.html"
    resp, _ := req.Get(url)
    selector, _ := htmlquery.Parse(strings.NewReader(resp.String()))
    itemList := htmlquery.Find(selector, `//ul[@class="item"]`)for _, item := range itemList {
        nameNode := htmlquery.FindOne(item, `li[@class="name"]/text()`)nameText := htmlquery.InnerText(nameNode)
        priceNode := htmlquery.FindOne(item, `li[@class="price"]/text()`)var priceText stringif priceNode != nil {priceText = htmlquery.InnerText(priceNode)
        } else {priceText = "0"
        }fmt.Println("商品名称:", nameText, "商品价格:", priceText)
    }
}

运行效果如下图所示:

efeefd963f50ac6ea2d080bac38d2118.png

在 Golang 里面使用 XPath,还是比 Python 中要麻烦不少。这一方面是因为 Python 是面向对象的语言,而 Golang 更像是面向过程的语言。另一方面是因为 Golang 的第三方库还是不如 Python 丰富。实际上是有办法可以让 Golang 的 XPath 库达到 lxml 这种易用性的。

参考资料

[1]

req: https://github.com/imroc/req

[2]

Chapter11_example_2: http://exercise.kingname.info/exercise_xpath_2.html

[3]

htmlquery: https://github.com/antchfx/htmlquery

79ca75659e7c1d243b5f2e805faf278a.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值