请求
Requests 所有的功能都可以通过以下 7 个方法访问。它们全部都会返回一个 Response 对象的实例。
方法 | 备注 |
---|---|
requests.request(method, url, **kwargs) | 一个顶六个 |
requests.head(url, **kwargs) | |
requests.get(url, params=None, **kwargs) | 传递URL参数 params=payload(a=1&b=1) |
requests.post(url, data=None, json=None, **kwargs) | data和json两个参数有所不同,发送数据格式有区别。data – (optional) Dictionary (will be form-encoded), bytes, or file-like object to send in the body of the Request. json – (optional) json data to send in the body of the Request. |
requests.put(url, data=None, **kwargs) | |
requests.patch(url, data=None, **kwargs) | |
requests.delete(url, **kwargs) |
定制请求头:
传递一个字典给headers参数
可以防反扒,如伪装客户端
代理:
proxies字段
proxies = {
“http”: “http://10.10.1.10:3128”,
“https”: “http://10.10.1.10:1080”,
}
响应
响应状态:
r.raise_for_status()
r.status_code == r.codes.ok
200
响应内容:
r.text 使用推测的文本编码,也可以指定
(r.encoding从响应header分析,r.apparent_encoding从内容分析,后者更准确)
r.content 二进制响应内容
r.json() JSON解码,处理JSON数据
r.headers 服务器响应头
r.cookies 响应中的cookie
原始响应内容:
设置stream=True
r.raw