Go语言 爬虫1-网络请求
2013-03-09 20:31
465 查看
下面是找的几个例子: 例子1:获得百度首页的html源文件:
package main
import(
"fmt"
"io/ioutil"
"net/http"
)
func main(){
response,_:=http.Get("http://www.baidu.com")
defer response.Body.Close()
body,_:=ioutil.ReadAll(response.Body)
fmt.Println(string(body))
}例子2,增加了一些错误验证代码来自:https://gist.github.com/ijt/950790
package main
import(
"fmt"
"io/ioutil"
"net/http"
"os"
)
func main(){
response,err:=http.Get("http://www.baidu.com/")
if err!=nil{
fmt.Printf("%s",err)
os.Exit(1)
}else{
defer response.Body.Close()
contents,err:=ioutil.ReadAll(response.Body)
if err!=nil{
fmt.Printf("%s",err)
os.Exit(1)
}
fmt.Printf("%s\n",string(contents))
}
}http下有Get,Post,PostForm三个函数。这三个函数直接实现了简单的http客户端下一个简单的例子增加了log, http://gameor.com/archives/178/golang%E5%8F%96%E9%93%BE%E6%8E%A5%E4%B8%8Ephp%E6%AF%94%E8%BE%83%E4%B8%8B/
package main
import(
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main(){
res,err:=http.Get("http://www.ghj1976.net/")
if err!=nil{
log.Fatal(err)
}
defer res.Body.Close()
robots,err:=ioutil.ReadAll(res.Body)
if err!=nil{
log.Fatal(err)
}
fmt.Printf("%s",robots)
}例子3:把百度的网页存在本地一个文件:/article/3890664.html
package main
import(
"fmt"
"log"
"net/http"
"os"
)
func main(){
resp,err:=http.Get("http://www.baidu.com")
if err!=nil{
//handleerror
fmt.Println(err)
log.Fatal(err)
}
defer resp.Body.Close()
if resp.StatusCode==http.StatusOK{
fmt.Println(resp.StatusCode)
}
buf:=make([]byte,1024)
//createfile
f,err1:=os.OpenFile("baidu.html",os.O_RDWR|os.O_CREATE|os.O_APPEND,os.ModePerm)
if err1!=nil{
panic(err1)
return
}
defer f.Close()
for{
n,_:=resp.Body.Read(buf)
if 0==n{
break
}
f.WriteString(string(buf[:n]))
}
}
其他可以借鉴的
golang 批量检查页面
http://www.simonzhang.net/?p=1346除了使用Get、Post、PostForm 这三个函数来建立一个简单客户端,还可以使用:http.Client和http.NewRequest来模拟请求例子:指定公共头的请求百度页面/article/4687473.html
package main
import(
"fmt"
"io/ioutil"
"net/http"
)
func main(){
client:=&http.Client{}
reqest,_:=http.NewRequest("GET","http://www.baidu.com",nil)
reqest.Header.Set("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
reqest.Header.Set("Accept-Charset","GBK,utf-8;q=0.7,*;q=0.3")
reqest.Header.Set("Accept-Encoding","gzip,deflate,sdch")
reqest.Header.Set("Accept-Language","zh-CN,zh;q=0.8")
reqest.Header.Set("Cache-Control","max-age=0")
reqest.Header.Set("Connection","keep-alive")
response,_:=client.Do(reqest)
if response.StatusCode==200{
body,_:=ioutil.ReadAll(response.Body)
bodystr:=string(body)
fmt.Println(bodystr)
}
}参考资料:用golang的正则regexp:去除HTML,CSS,SCRIPT代码,仅保留页面文字http://bpbp.iteye.com/blog/1668869 Golang解析网页入门http://mjason.github.com/blog/2013/01/29/golangjie-xi-wang-ye-ru-men/golang做的webCrawl: gocrawlhttp://hi.baidu.com/izouying/item/208551ec90a726f32a09a457一个Go语言实现的web爬虫http://www.sharejs.com/codes/go/4416
相关文章推荐
- Go语言Ubuntu下打印网络请求报文信息
- python爬虫 - python requests网络请求简洁之道
- go语言爬虫goquery和grequests的使用
- go语言之行--网络编程、http处理流程详情
- python3网络爬虫一《使用urllib.request发送请求》
- 从零实现一个高性能网络爬虫(一)网络请求分析及代码实现
- 网络爬虫中Jsoup请求url
- python爬虫 - python requests网络请求简洁之道
- Go语言 爬虫2-编码转换
- 03精通Python网络爬虫——HTTP协议请求实战
- 网络爬虫抓包使用及通过表单请求
- python 网络爬虫——请求头,ip代理
- go 语言http请求案列。
- Golang 网络爬虫框架gocolly/colly 四
- 使用python语言结合beautifulsoup编写简单的网络爬虫
- [Java语言] Java网络请求工具类
- 伪知识之通俗易懂得解释网络爬虫请求与响应的专有名词持续更新:【内向即失败--王奕君】
- 大数据:网络爬虫的post请求、get请求的区别
- 深入Go语言网络库的基础实现