内容简介:因为毕设模仿知乎做了个网站,需要点数据,所以打算爬点知乎的数据,本来想通过python写个爬虫,但是发现go也有个挺好用的爬虫库——goquery,如果你学过前端,那你完全可以在半个小时之内用goquery写出一个爬虫goquery类似jquery,它是jquery的go语言版本实现,使用它,可以很方便对HTML进行处理。它可以通过HTML Element元素,也可以通过Id选择器,Class选择器,以及属性选择器去筛选数据
goquery的使用
因为毕设模仿知乎做了个网站,需要点数据,所以打算爬点知乎的数据,本来想通过 python 写个爬虫,但是发现 go 也有个挺好用的爬虫库——goquery,如果你学过前端,那你完全可以在半个小时之内用goquery写出一个爬虫
goquery类似jquery,它是jquery的go语言版本实现,使用它,可以很方便对HTML进行处理。
它可以通过HTML Element元素,也可以通过Id选择器,Class选择器,以及属性选择器去筛选数据
github: https://github.com/PuerkitoBio/goquery
以下是我爬取知乎数据的demo代码
package main
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
_ "github.com/go-sql-driver/mysql"
)
func ExampleScrape() {
for i := 321450693; i > 321450680; i-- {
res, err := http.Get("https://www.zhihu.com/question/" + strconv.Itoa(i))
if err != nil || res.StatusCode != 200 {
continue
}
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
doc.Find(".QuestionHeader .QuestionHeader-content .QuestionHeader-main").Each(func(i int, s *goquery.Selection) {
questionTitle := s.Find(".QuestionHeader-title").Text()
questionContent := s.Find(".QuestionHeader-detail").Text()
questionContent = questionContent[0 : len(questionContent)-12]
fmt.Println("questionTitle:", questionTitle)
fmt.Println("questionContent:", questionContent)
})
doc.Find(".ContentItem-actions").Each(func(i int, s *goquery.Selection) {
})
doc.Find(".ListShortcut .List .List-item ").Each(func(i int, s *goquery.Selection) {
head_url, _ := s.Find("a img").Attr("src")
author := s.Find(".AuthorInfo-head").Text()
fmt.Println("head_url:", head_url)
fmt.Println("author:", author)
voters := s.Find(".Voters").Text()
voters = strings.Split(voters, " ")[0]
content := s.Find(".RichContent-inner").Text() //带标签的可以用Html()
createTime := s.Find(".ContentItem-time").Text()
createTime = strings.Split(createTime, " ")[1]
commentCount := s.Find(".ContentItem-actions span").Text()
fmt.Println("voters:", voters)
fmt.Println("content:", content)
fmt.Println("createTime:", createTime)
fmt.Println("commentCount : ", commentCount)
})
}
}
func main() {
ExampleScrape()
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- 一名数据小白,应该通过这篇文章了解大数据
- 通过海量数据挖掘新兴领域需求
- 通过“拖拽”搭建数据分析模型,「时代大数据」让经营管理者“直面数据”
- 如何通过 AB Testing 提升产品数据?
- 使用 SPSS 通过数据分析生成洞察
- 通过jdbc读取OBIEE数据--Spark示例
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Algorithms
Robert Sedgewick、Kevin Wayne / Addison-Wesley Professional / 2011-3-19 / USD 89.99
Essential Information about Algorithms and Data Structures A Classic Reference The latest version of Sedgewick,s best-selling series, reflecting an indispensable body of knowledge developed over the ......一起来看看 《Algorithms》 这本书的介绍吧!