-
Notifications
You must be signed in to change notification settings - Fork 0
/
tt.go
110 lines (101 loc) · 2.83 KB
/
tt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package main
import (
"fmt"
//"log"
"time"
"net/http"
"net/url"
"io/ioutil"
"strconv"
//"strings"
"encoding/json"
"math/rand"
//"github.com/PuerkitoBio/goquery"
)
//
func fetch(url *string, use_ip, ckaua string) (html string) {
fmt.Println(*url)
// proxy_addr,key,num :="","",0
// if use_ip == "1" {
// proxy_addr,key,num =getIp()
// }
//transport := getTransportFieldURL(&proxy_addr)
client := &http.Client{}//Transport : transport
//新建请求
req, err := http.NewRequest("GET", *url, nil)
if err != nil {
// log.Fatal(err.Error())
fmt.Println("新建失败")
html = ""
return
}
//设置请求头
if ckaua == "0" {
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2")
req.Header.Set("Cookie", "name=anny")
}else{
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2")
}
//
resp, err := client.Do(req)
if err != nil {
fmt.Println("请求失败")
html = ""
} else {
fmt.Println("11")
if resp.StatusCode == 200 {
fmt.Println("22")
robots, err := ioutil.ReadAll(resp.Body);
resp.Body.Close()
if err != nil {
fmt.Println("返回失败")
}
html = string(robots);
} else {
html = ""
}
}
return
}
//随机延时 ms
func delayRand(intime string){
r := rand.New(rand.NewSource(time.Now().UnixNano()))
tm,_ := strconv.Atoi(string(intime))
tt :=r.Intn(tm)
t := time.Duration(tt)
time.Sleep(1000000*t)
}
func worker(intime, use_ip, ckaua string, jobs <-chan *string, results chan<- string) {
for j := range jobs {
fmt.Println(*j)
html :=fetch(j,use_ip,ckaua)
fmt.Println("********")
fmt.Println(html)
delayRand(intime)
results <- html
}
}
func main() {
url :="http://www.zhugefang.com" //目标地址
use_ip := "1" //是否用IP代理 1:使用 0:不使用
ckaua := "1" //是否禁用cookie 1:禁用cookie 随机ua 0:记录cookie 固定ua
intime := "3" //最大时延 ms 不用延时设为0
//两个channel,一个用来放置工作项,一个用来存放处理结果。
jobs := make(chan *string, 100)
results := make(chan string, 100)
fmt.Println(url)
for w := 1; w <= 20000; w++ {
//是否使用IP代理
go worker(intime, use_ip, ckaua, jobs, results)
}
// 添加1000个任务后关闭Channel
for j := 1; j <= 100000; j++ {
//url=url+strconv.Itoa(j)
jobs <- &url
}
close(jobs)
//获取所有的处理结果
for a := 1; a <= 100000; a++ {
<-results
}
}