go?colly?爬蟲實(shí)現(xiàn)示例
正文
貢獻(xiàn)某CC,go源碼爬蟲一個(gè),基于colly,效果是根據(jù)輸入的瀏覽器cookie及excel必要行列號,從excel中讀取公司名稱,查詢公司法人及電話號碼。并寫回到excel中指定行。
package main
import (
"bufio"
"fmt"
"github.com/gocolly/colly/debug"
"github.com/gocolly/colly/extensions"
"github.com/xuri/excelize/v2"
"net/url"
"os"
"runtime"
"strconv"
"time"
)
import "github.com/gocolly/colly"
var (
cookies string
tempUrl string
tempGongSiName string
tempI int
)
func main() {
//要處理的文件全名
var fileName string
//列的名稱
var namelie string
//開始行號
var startNum int
//結(jié)束行號
var endNum int
var personLie string
var phoneLie string
fmt.Println("請輸入瀏覽器cookies 在瀏覽器 開發(fā)者模式F12,情況下找到控制臺(consol) 輸入(注意,Cookie中如果有 HttpOnly的需要在開發(fā)工具中將HttpOnly取消掉,然后再執(zhí)行后面命令):document.cookie 即可,然后復(fù)制出來! 右擊,復(fù)制字符串內(nèi)容")
//fmt.Scan(&cookies) //此行遇到空格會(huì) 默認(rèn)輸入完畢了,所以不能用它
reader := bufio.NewReader(os.Stdin)
res, _, err := reader.ReadLine()
if nil == err {
cookies=string(res)
}else{
fmt.Println("讀取cookie錯(cuò)誤 error:", err)
return
}
//fmt.Println("輸入的cookie是:"+cookies)
fmt.Println("請輸入文件全路徑:(字符串類型)")
fmt.Scan(&fileName)
fmt.Println("請輸入Excel要查詢公司名稱列的字母(字母大寫):")
fmt.Scan(&namelie)
fmt.Println("請輸入Excel指定列的第一個(gè)行號(數(shù)字類型):")
fmt.Scan(&startNum)
fmt.Println("請輸入Excel指定列的最后一個(gè)行號(數(shù)字類型):")
fmt.Scan(&endNum)
fmt.Println("請輸入Excel聯(lián)系人的所在列的字母(字母大寫):")
fmt.Scan(&personLie)
fmt.Println("請輸入Excel聯(lián)系電話所在列的字母(字母大寫):")
fmt.Scan(&phoneLie)
//輸出所有輸入的信息,驗(yàn)證正確
//fmt.Println(fileName,namelie,startNum,endNum,personLie,phoneLie)
f, err := excelize.OpenFile(fileName)
if err!=nil {
fmt.Println(err)
return
}
c:=initCollector(f,personLie,phoneLie)
//上面打開的工作簿記得關(guān)閉吆。
defer func() {
// 關(guān)閉工作簿
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
for i:=startNum;i<=endNum;i++{
// 獲取工作表中指定單元格的值
cell, err := f.GetCellValue("Sheet1", namelie+strconv.Itoa(i))
if err != nil {
fmt.Println("讀取第"+strconv.Itoa(i)+"行出錯(cuò)!")
return
}else{
fmt.Println("開始抓取:"+cell+" 數(shù)據(jù)")
tempGongSiName = cell
tempI = i
visitUrl(c)
time.Sleep(1*time.Second)
}
}
fmt.Println("-------------親愛的,程序成功執(zhí)行完畢。--------我要喝咖啡,我要吃肉肉------!")
}
///初始化收集器
func initCollector(f *excelize.File,personLie string,phoneLie string,) *colly.Collector {
c := colly.NewCollector(colly.MaxDepth(1), colly.Debugger(&debug.LogDebugger{}))
extensions.RandomUserAgent(c) // 使用隨機(jī)的UserAgent,最好能使用代理。這樣就不容易被ban
c.SetProxy("socks5://127.0.0.1:7890")
c.OnError(func(response *colly.Response, err error) {
fmt.Println("---->onError --------爬取出錯(cuò)了"+err.Error())
runtime.Goexit()
})
c.OnResponse(func(response *colly.Response) {
fmt.Println("---->onResponse")
})
c.OnXML("table", func(element *colly.XMLElement) {
fmt.Println("---->onXML")
})
c.OnRequest(func(r *colly.Request) {
r.Headers.Set("Cookie",cookies)
r.Headers.Add("referer", tempUrl)
r.Headers.Add("sec-fetch-mode", "cors")
r.Headers.Add("sec-fetch-site", "same-origin")
r.Headers.Add("accept", "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01")
r.Headers.Add("accept-encoding", "gzip, deflate, br")
r.Headers.Add("accept-language", "en,zh-CN;q=0.9,zh;q=0.8")
r.Headers.Add("X-Requested-With", "XMLHttpRequest")
})
c.OnHTML("tr:first-child", func(e *colly.HTMLElement) {//拿到查詢的第一條數(shù)據(jù)。
fmt.Println("---->onHtml---獲取成功!")
//拿到第一條的公司主要信息。
//fmt.Println("---->"+e.DOM.Find(".relate-info").Text())
sellectEle := e.DOM.Find(".relate-info")
//最終查詢出來的人
name:=sellectEle.Find("div:nth-child(1)").Find("div>span").First().Find("a").Text()
//最終查詢出來的電話
phone:=sellectEle.Find("div:nth-child(2)").Find("div>span").First().Find("span>span").Find(":nth-child(2)").Text()
//fmt.Println("--->>>"+name)
//fmt.Println("--->>>"+phone)
f.SetCellValue("Sheet1", personLie+strconv.Itoa(tempI), name)
fmt.Println("將"+tempGongSiName+"人名 ("+name+") 寫入 "+personLie+strconv.Itoa(tempI))
f.SetCellValue("Sheet1", phoneLie+strconv.Itoa(tempI), phone)
fmt.Println("將"+tempGongSiName+"電話 ("+phone+") 寫入 "+phoneLie+strconv.Itoa(tempI))
f.Save()
})
c.OnScraped(func(response *colly.Response) {
fmt.Println("onScraped")
})
return c
}
//訪問給定名稱
func visitUrl(c *colly.Collector){
tempUrl:="https://www.xxx.com/web/search?key="+url.QueryEscape(tempGongSiName)
c.Visit(tempUrl)
}以上就是go colly 爬蟲實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于go colly 爬蟲的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Golang監(jiān)聽日志文件并發(fā)送到kafka中
這篇文章主要介紹了Golang監(jiān)聽日志文件并發(fā)送到kafka中,日志收集項(xiàng)目的準(zhǔn)備中,本文主要講的是利用golang的tail庫,監(jiān)聽日志文件的變動(dòng),將日志信息發(fā)送到kafka中?,需要的朋友可以參考一下2022-04-04
Go實(shí)現(xiàn)MD5加密的三種方法小結(jié)
本文主要介紹了Go實(shí)現(xiàn)MD5加密的三種方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Golang實(shí)現(xiàn)四種負(fù)載均衡的算法(隨機(jī),輪詢等)
本文介紹了示例介紹了Golang 負(fù)載均衡的四種實(shí)現(xiàn),主要包括了隨機(jī),輪詢,加權(quán)輪詢負(fù)載,一致性hash,感興趣的小伙伴們可以參考一下2021-06-06
GoFrame錯(cuò)誤處理常用方法及錯(cuò)誤碼使用示例
這篇文章主要為大家介紹了GoFrame錯(cuò)誤處理常用方法及錯(cuò)誤碼使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
golang實(shí)現(xiàn)簡單rpc調(diào)用過程解析
這篇文章主要介紹了golang實(shí)現(xiàn)簡單rpc調(diào)用,包括RPC具體實(shí)現(xiàn)結(jié)合實(shí)例代碼給大家講解的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05

