写了一个基于 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" } ```