在爬虫中,你发现你的模拟已经很完美了,但是还是拿不到预期数据。怎么办呢?你可以对比一下,网站的报文跟你爬虫发出去的报文是不是一致的。
我们可以利用 httpbin 这个网站,先来看一下 requests 发出的 HTTP 报文默认的请求头是什么样子的。
可以这样测试:
import requests
url = 'http://httpbin.org/get'
r = requests.get (url)
print r.content
返回结果:
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.7.0 CPython/2.7.10 Windows/7"
},
"origin": "xx.xx.xx.xx",
"url": "http://httpbin.org/get"
}
上面例子主要是让发送者清楚知道最终给服务器发送了写什么内容。
httpbin给我们提供了很多HTTP基本的操作,比如测试发出去的cookies,ua等,也支持get,post等,常用接口我列在下面:
获取IP: http://httpbin.org/ip
获取UA: http://httpbin.org/user-agent
获取header: http://httpbin.org/headers
获取cookies: http://httpbin.org/cookies
还有很多可以去官网看看:http://httpbin.org/
使用这个还有个技巧,用于前后端对接,
如果你的APP组问你要接口,你手头又很忙,怎么办?当然是用httpbin按照接口文档先给他们快速写几个丢过去。