通过goquery爬取知乎数据

栏目: Go · 发布时间: 7年前

内容简介:因为毕设模仿知乎做了个网站,需要点数据,所以打算爬点知乎的数据,本来想通过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()
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

HTML5

HTML5

Matthew David / Focal Press / 2010-07-29 / USD 39.95

Implement the powerful new multimedia and interactive capabilities offered by HTML5, including style control tools, illustration tools, video, audio, and rich media solutions. Understand how HTML5 is ......一起来看看 《HTML5》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换