-
Notifications
You must be signed in to change notification settings - Fork 33
/
go_carw01.go
54 lines (49 loc) · 1.15 KB
/
go_carw01.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
//指定代理ip
func getTransportFieldURL(proxy_addr *string) (transport *http.Transport) {
url_i := url.URL{}
url_proxy, _ := url_i.Parse(*proxy_addr)
transport = &http.Transport{Proxy: http.ProxyURL(url_proxy)}
return
}
//从环境变量$http_proxy或$HTTP_PROXY中获取HTTP代理地址
func getTransportFromEnvironment() (transport *http.Transport) {
transport = &http.Transport{Proxy: http.ProxyFromEnvironment}
return
}
func fetch(url, proxy_addr *string) (html string) {
transport := getTransportFieldURL(proxy_addr)
client := &http.Client{Transport: transport}
req, err := http.NewRequest("GET", *url, nil)
if err != nil {
log.Fatal(err.Error())
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err.Error())
}
if resp.StatusCode == 200 {
robots, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatal(err.Error())
}
html = string(robots)
} else {
html = ""
}
return
}
func main() {
proxy_addr := "http://183.221.250.137:80/"
url := "http://www.baidu.com/s?wd=ip"
html := fetch(&url, &proxy_addr)
fmt.Println(html)
}