get
请求网址与传递参数
1 | payload = {'key1': 'value1', 'key2': 'value2'} |
定制请求头
1 | headers = {'user-agent': 'my-app/0.0.1'} |
超时
1 | r = requests.get(url, timeout=0.001) |
短链接
1 | r = requests.get(url,headers={'Connection':'close'}) |
post
发送表单数据
1 | data = {'key':'value'} |
发送json数据
1 | # requests会自动序列化data |
处理返回结果
1 | # 得到json |
代理ip
1 | import requests |
编码与解码问题
我们可以通过 r.encoding
查看requests抓取文件的编码
1 | r = requests.get(url) |
如果 Requests 检测不到正确的编码,那么你告诉它正确的是什么:
1 | response.encoding = 'gbk' |
字符串在Python内部的表示是 unicode
编码,因此,在做编码转换时,通常需要以 unicode
作为中间编码,即先将其他编码的字符串解码 decode
成 unicode
,再从 unicode
编码 encode
成另一种编码。
decode
的作用是将其他编码的字符串转换成unicode编码,如 str1.decode('gb2312')
,表示将 gb2312
编码的字符串 str1
转换成 unicode
编码。
encode
的作用是将 unicode
编码转换成其他编码的字符串,如 str2.encode('gb2312')
,表示将 unicode
编码的字符串 str2
转换成 gb2312
编码。
如果一个字符串已经是 unicode
了,再进行解码则将出错,因此通常要对其编码方式是否为 unicode
进行判断,用非 unicode
编码形式的str来 encode
会报错