Golang網(wǎng)絡(luò)爬蟲(chóng)框架gocolly/colly怎么用-創(chuàng)新互聯(lián)

小編給大家分享一下Golang網(wǎng)絡(luò)爬蟲(chóng)框架gocolly/colly怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

你所需要的網(wǎng)站建設(shè)服務(wù),我們均能行業(yè)靠前的水平為你提供.標(biāo)準(zhǔn)是產(chǎn)品質(zhì)量的保證,主要從事網(wǎng)站制作、成都網(wǎng)站制作、企業(yè)網(wǎng)站建設(shè)、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)制作、做網(wǎng)站、建網(wǎng)站。創(chuàng)新互聯(lián)建站擁有實(shí)力堅(jiān)強(qiáng)的技術(shù)研發(fā)團(tuán)隊(duì)及素養(yǎng)的視覺(jué)設(shè)計(jì)專才。

安裝

go get -u github.com/gocolly/colly/...

例子

import (    "fmt"    "github.com/gocolly/colly")func main() {    c := colly.NewCollector()    c.OnResponse(func(r *colly.Response) {        fmt.Println("IP:", string(r.Body))    })    //c.SetProxy("http://127.0.0.1:1080")    c.Visit("http://ip.cip.cc/")}

SetProxy函數(shù)可以用來(lái)配置HTTP代理。

colly的主體是Collector對(duì)象,管理網(wǎng)絡(luò)通信和負(fù)責(zé)在作業(yè)運(yùn)行時(shí)執(zhí)行附加的回掉函數(shù)。使用colly需要先初始化Collector:

c := colly.NewCollector()

vgo

vgo是Go語(yǔ)言推出的第三方庫(kù)管理工具,在Go語(yǔ)言新版本中使用。

常用的命令行:

· go help mod 查看幫助。

· go mod init <項(xiàng)目模塊名稱>初始化模塊,會(huì)在項(xiàng)目根目錄下生成 go.mod 文件,是可以自己手動(dòng)編輯的。

依賴包大多在Github上,安裝依賴可能會(huì)出現(xiàn)連接超時(shí)等問(wèn)題,可以配置全局git代理:

git config --global http.proxy http://127.0.0.1:1080git config --global https.proxy https://127.0.0.1:1080#取消代理:git config --global --unset http.proxygit config --global --unset https.proxy

cmd走shadowsocks代理:

set http_proxy=127.0.0.1:1080set https_proxy=127.0.0.1:1080curl cip.ccIP    : 140.206.97.42地址    : 中國(guó)  上海數(shù)據(jù)二    : 上海市 | 聯(lián)通URL    : http://www.cip.cc/140.206.97.42

Linux使用export設(shè)置環(huán)境變量,代碼同上。

回掉函數(shù)的調(diào)用順序

1. OnRequest 在發(fā)起請(qǐng)求前被調(diào)用。

2. OnError 請(qǐng)求過(guò)程中如果發(fā)生錯(cuò)誤被調(diào)用。

3. OnResponse 收到回復(fù)后被調(diào)用。

4. OnHTML 在OnResponse之后被調(diào)用,如果收到的內(nèi)容是HTML 。

5. OnScraped 在OnHTML之后被調(diào)用。

官方提供的Basic示例代碼:

package mainimport (    "fmt"    "github.com/gocolly/colly")func main() {    // Instantiate default collector    c := colly.NewCollector(        // Visit only domains: hackerspaces.org, wiki.hackerspaces.org        colly.AllowedDomains("hackerspaces.org", "wiki.hackerspaces.org"),    )    // On every a element which has href attribute call callback    c.OnHTML("a[href]", func(e *colly.HTMLElement) {        link := e.Attr("href")        // Print link        fmt.Printf("Link found: %q -> %s\n", e.Text, link)        // Visit link found on page        // Only those links are visited which are in AllowedDomains         c.Visit(e.Request.AbsoluteURL(link))    })    // Before making a request print "Visiting ..."    c.OnRequest(func(r *colly.Request) {        fmt.Println("Visiting", r.URL.String())    })    // Start scraping on https://hackerspaces.org    c.Visit("https://hackerspaces.org/")}

該實(shí)例程序僅訪問(wèn)hackerspaces.org域內(nèi)的鏈接,OnHTML回掉函數(shù)的選擇器為a[href],選擇頁(yè)面內(nèi)具有href屬性的a類型元素,找到鏈接后繼續(xù)抓取。 運(yùn)行的部分結(jié)果如下:

Visiting https://hackerspaces.org/Link found: "navigation" -> #column-oneLink found: "search" -> #searchInputLink found: "" -> /File:Cbase07.jpgVisiting https://hackerspaces.org/File:Cbase07.jpgLink found: "navigation" -> #column-oneLink found: "search" -> #searchInputLink found: "File" -> #fileLink found: "File history" -> #filehistoryLink found: "File usage" -> #filelinksLink found: "" -> /images/e/ec/Cbase07.jpgVisiting https://hackerspaces.org/images/e/ec/Cbase07.jpgLink found: "800 × 600 pixels" -> /images/thumb/e/ec/Cbase07.jpg/800px-Cbase07.jpgVisiting https://hackerspaces.org/images/thumb/e/ec/Cbase07.jpg/800px-Cbase07.jpg

以上是“Golang網(wǎng)絡(luò)爬蟲(chóng)框架gocolly/colly怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!

分享名稱:Golang網(wǎng)絡(luò)爬蟲(chóng)框架gocolly/colly怎么用-創(chuàng)新互聯(lián)
本文路徑:http://www.muchs.cn/article30/ipcso.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航全網(wǎng)營(yíng)銷推廣、云服務(wù)器網(wǎng)站設(shè)計(jì)公司、品牌網(wǎng)站設(shè)計(jì)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名