前端之家收集整理的这篇文章主要介绍了
Golang通过http代理抓取页面代码,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main
import (
"fmt"
"io/IoUtil"
"net/http"
"net/url"
)
// http get by proxy
func GetByProxy(url_addr,proxy_addr string) (*http.Response,error) {
request,_ := http.NewRequest("GET",url_addr,nil)
proxy,err := url.Parse(proxy_addr)
if err != nil {
return nil,err
}
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyURL(proxy),},}
return client.Do(request)
}
func main() {
proxy := "http://58.252.56.149:9000/"
url := "http://www.baidu.com/"
resp,_ := GetByProxy(url,proxy)
fmt.Println(resp)
defer resp.Body.Close()
body,_ := IoUtil.ReadAll(resp.Body)
fmt.Println(string(body))
}