GolangNote

Golang笔记

Golang 爬虫工具 goquery 推荐

Permalink

goquery 是基于标准库net/html 实现的用于解析 HTML 的库,是使用 jQuery 的方式去操作 DOM。

使用很简单:

Go: goquery 示例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package main

import (
    // import standard libraries
    "fmt"
    "log"

    // import third party libraries
    "github.com/PuerkitoBio/goquery"
)

func postScrape() {
    doc, err := goquery.NewDocument("https://www.golangnote.com")
    if err != nil {
        log.Fatal(err)
    }

    // use CSS selector found with the browser inspector
    // for each, use index and item
    doc.Find(".topic-items article header h1").Each(func(index int, item *goquery.Selection) {
        title := item.Text()
        linkTag := item.Find("a")
        link, _ := linkTag.Attr("href")
        fmt.Printf("Post #%d: %s - %s\n", index, title, link)
    })
}

func main() {
    postScrape()
}

输出:

plaintext: goquery 示例输出
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Post #0: golang http 转向状态码的小区别 - http://www.golangnote.com/topic/198.html
Post #1: golang 获取用户真实访问ip 地址 - http://www.golangnote.com/topic/197.html
Post #2: 推荐几款开源免费的用golang写的论坛程序 - http://www.golangnote.com/topic/196.html
Post #3: Golang io.ReadCloser 和[]byte 相互转化的方法 - http://www.golangnote.com/topic/195.html
Post #4: golang 截取中文不出现乱码的方法 - http://www.golangnote.com/topic/194.html
Post #5: Golang 用标准库把图片做成gif动画 - http://www.golangnote.com/topic/193.html
Post #6: golang 获取程序本身的md5值 - http://www.golangnote.com/topic/192.html
Post #7: golang 基于bolt 做一个支持hash、zet 数据结构 - http://www.golangnote.com/topic/191.html
Post #8: Golang 定时循环的实现 - http://www.golangnote.com/topic/190.html
Post #9: goji 路由组、反向代理、中间件简单示例 - http://www.golangnote.com/topic/189.html
Post #10: Golang 合并byte 的性能比较 - http://www.golangnote.com/topic/188.html
Post #11: Golang string 和byte 操作性能比较 - http://www.golangnote.com/topic/187.html
Post #12: Golang byte to string 的方法实践 - http://www.golangnote.com/topic/186.html
Post #13: Golang 简单的任务队列 - http://www.golangnote.com/topic/185.html
Post #14: Golang 并发控制的两种模式 - http://www.golangnote.com/topic/184.html
Post #15: Golang 切割pdf 文件并取出前三页 - http://www.golangnote.com/topic/183.html

本文网址: https://golangnote.com/topic/199.html 转摘请注明来源

Related articles

谷歌翻译的 golang推荐

Google 的翻译越来越好了,官方的Golang SDK 已经很完美,这里介绍的是几个非官方发布的有特色的库。...

Golang 生成防识别的图片验证码

验证码 captcha 是对抗密码强力破解、垃圾信息的有效方式,一般用于用户注册、登录,当检测到频繁发帖时也会启用验证码。下面介绍用golang 生成防机器识别的图片验证码。...

Write a Comment to "Golang 爬虫工具 goquery 推荐"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 0ms