1. 首页
  2. 主题
  3. Go语言

基于Golang的HTTP客户端,爬虫工具direwolf

wnanbei · · 1664 次点击
写了一个基于 Golang 的 HTTP 客户端,可以做为爬虫工具。因为用 Golang 发请求的时候发现有些设置很麻烦,所以参考 Python 的 Requests,写了这个工具。 地址: https://wnanbei.github.io/direwolf/ 网站中有完整的中文文档,后续会继续完善和添加新的功能。有兴趣的朋友欢迎在 Gayhub 点个 star,如果有 BUG、问题或者建议,也可以在 Gayhub 的 Issues 页面与我讨论。 以下是部分简单用法的展示: 你可以像下方这样非常简单的发起一个请求: ```go import ( "fmt" dw "github.com/wnanbei/direwolf" ) func main() { resp, err := dw.Get("http://httpbin.org/get") if err != nil { return } fmt.Println(resp.Text()) } ``` 输出: ```json { "args": {}, "headers": { "Accept-Encoding": "gzip", "Host": "httpbin.org", "User-Agent": "direwolf - winter is coming" }, "origin": "171.217.52.188, 171.217.52.188", "url": "https://httpbin.org/get" } ``` 除此之外,direwolf 可以很方便的给一个请求添加参数,例如 Headers、Cookies、Params。 ```go import ( "fmt" dw "github.com/wnanbei/direwolf" ) func main() { headers := dw.NewHeaders( "User-Agent", "direwolf", ) params := dw.NewParams( "name", "wnanbei", "age", "18", ) cookies := dw.NewCookies( "sign", "kzhxciuvyqwekhiuxcyvnkjdhiue", ) resp, err := dw.Get("https://httpbin.org/get", headers, params, cookies) if err != nil { return } fmt.Println(resp.Text()) } ``` 输出: ```json { "args": { "age": "18", "name": "wnanbei" }, "headers": { "Accept-Encoding": "gzip", "Cookie": "sign=kzhxciuvyqwekhiuxcyvnkjdhiue", "Host": "httpbin.org", "User-Agent": "direwolf" }, "origin": "1.1.1.1, 1.1.1.1", "url": "https://httpbin.org/get?age=18&name=wnanbei" } ```
读数据报错 unexpected EOF 如果加> "Accept-Encoding": "gzip", 用resp.Content读出来的是字节,用string(resp.Content()) 是乱码 已提交github issue
#4
更多评论
如果客户端要维护长连接 该怎么写呢 https://wnanbei.github.io/direwolf/docs/tutorial#1-make-request 上没找到 另外如果发送 http头dw.NewHeaders( "Accept-Encoding", "gzip, deflate") 用 "compress/gzip" 的reader, err := gzip.NewReader(resp.Text()) 读取 ,提示 cannot use resp.Text() (type string) as type io.Reader in argument to gzip.NewReader: string does not implement io.Reader (missing Read method) 比如读这个页面 url="https://blog.csdn.net/yang731227/article/details/89338745" 读resp.Content() 提示 cannot use resp.Content() (type []byte) as type io.Reader in argument to gzip.NewReader: []byte does not implement io.Reader (missing Read method)
#2

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中