小編給大家分享一下Golang網(wǎng)絡(luò)爬蟲框架gocolly/colly怎么用,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
你所需要的網(wǎng)站建設(shè)服務(wù),我們均能行業(yè)靠前的水平為你提供.標(biāo)準(zhǔn)是產(chǎn)品質(zhì)量的保證,主要從事網(wǎng)站制作、成都網(wǎng)站制作、企業(yè)網(wǎng)站建設(shè)、移動網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、品牌網(wǎng)站設(shè)計、網(wǎng)頁制作、做網(wǎng)站、建網(wǎng)站。創(chuàng)新互聯(lián)建站擁有實力堅強的技術(shù)研發(fā)團隊及素養(yǎng)的視覺設(shè)計專才。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ù)可以用來配置HTTP代理。
colly的主體是Collector對象,管理網(wǎng)絡(luò)通信和負(fù)責(zé)在作業(yè)運行時執(zhí)行附加的回掉函數(shù)。使用colly需要先初始化Collector:
c := colly.NewCollector()
vgo是Go語言推出的第三方庫管理工具,在Go語言新版本中使用。
· go help mod 查看幫助。
· go mod init <項目模塊名稱>初始化模塊,會在項目根目錄下生成 go.mod 文件,是可以自己手動編輯的。
依賴包大多在Github上,安裝依賴可能會出現(xià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地址 : 中國 上海數(shù)據(jù)二 : 上海市 | 聯(lián)通URL : http://www.cip.cc/140.206.97.42
Linux使用export設(shè)置環(huán)境變量,代碼同上。
1. OnRequest 在發(fā)起請求前被調(diào)用。
2. OnError 請求過程中如果發(fā)生錯誤被調(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/")}
該實例程序僅訪問hackerspaces.org域內(nèi)的鏈接,OnHTML回掉函數(shù)的選擇器為a[href],選擇頁面內(nèi)具有href屬性的a類型元素,找到鏈接后繼續(xù)抓取。 運行的部分結(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ò)爬蟲框架gocolly/colly怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道!
分享名稱:Golang網(wǎng)絡(luò)爬蟲框架gocolly/colly怎么用-創(chuàng)新互聯(lián)
本文路徑:http://bm7419.com/article30/ipcso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、全網(wǎng)營銷推廣、云服務(wù)器、網(wǎng)站設(shè)計公司、品牌網(wǎng)站設(shè)計、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容