### 快代理2
gocolly包github官網:https://github.com/gocolly/colly
gocolly官網:http://go-colly.org/
快代理:"https://www.kuaidaili.com/free
首先安裝goclly這個包,老方法,去github復制它示例代理,然后去項目文件打開cmd,使用指令go get獲取
示例代碼:
```
package main
import (
"fmt"
"github.com/gocolly/colly"
)
func main() {
c := colly.NewCollector()
// Find and visit all links
c.OnHTML("a[href]", func(e *colly.HTMLElement) {
e.Request.Visit(e.Attr("href"))
})
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting", r.URL)
})
c.Visit("http://go-colly.org/")
}
```
這次爬取多頁

```
package main
import (
"fmt"
"strconv"
"time"
"github.com/gocolly/colly"
)
// 創建一個結構體 用于json數據的接收
type Datalist struct {
Ip string `json:"ip"` // ip
}
// 創建一個結構體 用于json數據的接收
type Datalist2 struct {
Port string `json:"port"` // port 端口
}
// 創建一個結構體 用于json數據的接收
type Datalist3 struct {
Anonymous string `json:"anonymous"` // 匿名度
}
// 創建一個結構體 用于json數據的接收
type Datalist4 struct {
Agreement string `json:"agreement"` //類型
}
// 創建一個結構體 用于json數據的接收
type Datalist5 struct {
Region string `json:"region"` //地區
}
// 創建一個結構體 用于json數據的接收
type Datalist6 struct {
Speed string `json:"speed"` //速度
}
// 創建一個結構體 用于json數據的接收
type Datalist7 struct {
Timeout string `json:"timeout"` //最后驗證時間
}
// 再創一個結構體 匯總一下
type Presult struct {
Ips string `json:"ips"` // ip
Ports string `json:"ports"` // port 端口
Anonymouss string `json:"anonymouss"` // 匿名度
Agreements string `json:"agreements"` //類型
Regions string `json:"regions"` //地區
Speeds string `json:"speeds"` //速度
Timeouts string `json:"timeouts"` //最后驗證時間
}
func Collection() {
// 使用結構題傳入
proxyList := []Datalist{}
proxyList2 := []Datalist2{}
proxyList3 := []Datalist3{}
proxyList4 := []Datalist4{}
proxyList5 := []Datalist5{}
proxyList6 := []Datalist6{}
proxyList7 := []Datalist7{}
Prelist := []Presult{}
//初始化 設置一個請求頭 瀏覽器標識
c := colly.NewCollector(
colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"))
// 發出請求時附的回調
c.OnRequest(func(r *colly.Request) {
// request頭部設置
r.Headers.Set("Content-Type", "application/")
})
// 請求打印狀態碼
c.OnResponse(func(r *colly.Response) {
fmt.Println("響應狀態:", r.StatusCode)
})
// 對響應的html元素處理
c.OnHTML("title", func(e *colly.HTMLElement) {
fmt.Println("title:", e.Text)
})
// 對響應的html元素處理
c.OnHTML("table tbody", func(e *colly.HTMLElement) {
// 爬取第一個 td標簽的內容
e.ForEach("td:nth-of-type(1)", func(i int, element *colly.HTMLElement) {
ip := element.Text
proxyList = append(proxyList, Datalist{
Ip: ip,
})
})
e.ForEach("td:nth-of-type(2)", func(i int, element *colly.HTMLElement) {
port := element.Text
proxyList2 = append(proxyList2, Datalist2{
Port: port,
})
})
e.ForEach("td:nth-of-type(3)", func(i int, element *colly.HTMLElement) {
anonymous := element.Text
proxyList3 = append(proxyList3, Datalist3{
Anonymous: anonymous,
})
})
e.ForEach("td:nth-of-type(4)", func(i int, element *colly.HTMLElement) {
agreement := element.Text
proxyList4 = append(proxyList4, Datalist4{
Agreement: agreement,
})
})
e.ForEach("td:nth-of-type(5)", func(i int, element *colly.HTMLElement) {
region := element.Text
proxyList5 = append(proxyList5, Datalist5{
Region: region,
})
})
e.ForEach("td:nth-of-type(6)", func(i int, element *colly.HTMLElement) {
speed := element.Text
proxyList6 = append(proxyList6, Datalist6{
Speed: speed,
})
})
e.ForEach("td:nth-of-type(7)", func(i int, element *colly.HTMLElement) {
timeout := element.Text
proxyList7 = append(proxyList7, Datalist7{
Timeout: timeout,
})
})
for i, _ := range proxyList {
Prelist = append(Prelist, Presult{
Ips: proxyList[i].Ip, //ip
Ports: proxyList2[i].Port, // port 端口
Anonymouss: proxyList3[i].Anonymous, // 匿名度
Agreements: proxyList4[i].Agreement, //類型
Regions: proxyList5[i].Region, //地區
Speeds: proxyList6[i].Speed, //速度
Timeouts: proxyList7[i].Timeout, //最后驗證時間
})
}
fmt.Println(Prelist)
})
// 請求出錯時
c.OnError(func(r *colly.Response, e error) {
fmt.Println(e.Error())
})
// 設置線程數
c.Limit(&colly.LimitRule{
Parallelism: 2,
})
// 爬取前3頁 每頁停留5秒
for i := 1; i <= 3; i++ {
c.Visit("https://www.kuaidaili.com/free/inha/" + strconv.Itoa(i) + "/")
time.Sleep(time.Second * 5)
}
}
func main() {
Collection()
}
```
- 安裝開發環境
- 安裝開發環境
- 安裝詳細教程
- 引入包
- Go語言基礎
- 基本變量與數據類型
- 變量
- 數據類型
- 指針
- 字符串
- 代碼總結
- 常量與運算符
- 常量
- 運算符
- 流程控制
- if判斷
- for循環
- switch分支
- goto跳轉
- 斐波那契數列
- Go語言內置容器
- 數組
- 切片
- 映射
- 函數
- 函數(上)
- 函數(中)
- 函數(下)
- 小節
- 包管理
- 結構體
- 結構體(上)
- 結構體(中)
- 結構體(下)
- 小節
- 錯誤處理
- 錯誤處理
- 宕機
- 錯誤應用
- 小節
- 文件操作
- 獲取目錄
- 創建和刪除目錄
- 文件基本操作(上)
- 文件基本操作(中)
- 文件基本操作(下)
- 處理JSON文件
- 接口與類型
- 接口的創建與實現
- 接口賦值
- 接口嵌入
- 空接口
- 類型斷言(1)
- 類型斷言(2)
- 小節
- 并發與通道
- goroutine協程
- runtime包
- 通道channel
- 單向通道channel
- select
- 線程同步
- 多線程的深入學習
- http編程
- http簡介
- Client和Request
- get請求
- post請求
- 模塊函數方法
- 模塊
- fmt庫,模塊
- 項目練習
- 爬蟲:高三網
- 爬蟲:快代理
- 爬蟲:快代理2
- 多線程:通道思路
- 多線程爬蟲:快代理