import requests
response=requests.get('http://www.baidu.com',params=KW,headers)
print(response.text)#输出的是requests自己解码的数据,并不一定符合我们想要的
print(response.content)#输出的是bytes字节流数据 解码response.content.decoding("utf-8")
print(response.url) #输出请求的链接
print(response.status_code) #输出请求的状态码
response.text 与 response.content的区别:
1.response.content 这个是直接从网络上抓取的数据,没有经过任何的解码,所以是bytes类型。硬盘上和网络中传输的都是bytes类型。
2.response.text:这个是str的数据类型是requests库将response.content进行解码的字符串,解码需要指定一个编码方式。
requests会根据自己的猜测判断内容的编码方式,可能会出错,导致解码出现乱码,这时可以使用
response.content.decode('utf-8')
发送post请求:
response=requests.post("https://www.baidu.com",data=data,headers=headers)
如果返回的是json数据,那么可以调用request.json()
将json字符串转化为字典或者列表
requests使用代理IP:
import requests
url="http://www.httpbin.org/get"
headers={
"User-Agent":"……"
}
proxy={
'http:':'172.152.141.54:9999'
}
resp=requests.get(url,headers=headers,proxies=proxy)
with open('html.html','w',encoding='utf-8') as fp:
fp.write(resp.text)
处理cookies:
处理没有授权的https协议: