requeses库
安装
pip install requests
中文文档:http://docs.python-requests.org/zh_CN/latest/index.html
github地址:https://github.com/requests/requests
import requests
发送get请求
最简单的发送get请求就是通过requests.get来调用
response = requests.get('http://www.baidu.com/')
response
<Response [200]>
添加headers和查询参数
如果想添加 headers,可以传入headers参数来增加请求头中的headers信息。如果要将参数放在url中传递,可以利用 params 参数。相关示例代码如下
kw = {'wd':'中国'}
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
# params 接收一个字典或者字符串的查询参数,字典类型自动转换为url编码,不需要urlencode()
response = requests.get("http://www.baidu.com/s", params = kw, headers = headers)
# 查看响应内容,response.text 返回的是Unicode格式的数据
print(response.text)
# 查看响应内容,response.content返回的字节流数据
print(response.content)
b'<!DOCTYPE html>\n<html lang="zh-CN">\n<head>\n <meta charset="utf-8">\n <title>\xe7\x99\xbe\xe5\xba\xa6\xe5\xae\x89\xe5\x85\xa8\xe9\xaa\x8c\xe8\xaf\x81</title>\n <meta http-equiv="Content-Type" content="text/html; charset=utf-8">\n <meta name="apple-mobile-web-app-capable" content="yes">\n <meta name="apple-mobile-web-app-status-bar-style" content="black">\n <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0">\n <meta name="format-detection" content="telephone=no, email=no">\n <link rel="shortcut icon" href="https://www.baidu.com/favicon.ico" type="image/x-icon">\n <link rel="icon" sizes="any" mask href="https://www.baidu.com/img/baidu.svg">\n <meta http-equiv="X-UA-Compatible" content="IE=Edge">\n <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">\n <link rel="stylesheet" href="https://wappass.bdimg.com/static/touch/css/api/mkdjump_8befa48.css" />\n</head>\n<body>\n <div class="timeout hide">\n <div class="timeout-img"></div>\n <div class="timeout-title">\xe7\xbd\x91\xe7\xbb\x9c\xe4\xb8\x8d\xe7\xbb\x99\xe5\x8a\x9b\xef\xbc\x8c\xe8\xaf\xb7\xe7\xa8\x8d\xe5\x90\x8e\xe9\x87\x8d\xe8\xaf\x95</div>\n <button type="button" class="timeout-button">\xe8\xbf\x94\xe5\x9b\x9e\xe9\xa6\x96\xe9\xa1\xb5</button>\n </div>\n <div class="timeout-feedback hide">\n <div class="timeout-feedback-icon"></div>\n <p class="timeout-feedback-title">\xe9\x97\xae\xe9\xa2\x98\xe5\x8f\x8d\xe9\xa6\x88</p>\n </div>\n\n<script src="https://wappass.baidu.com/static/machine/js/api/mkd.js"></script>\n<script src="https://wappass.bdimg.com/static/touch/js/mkdjump_2e06726.js"></script>\n</body>\n</html>'
# 查看完整url地址
print(response.url)
https://wappass.baidu.com/static/captcha/tuxing.html?&ak=c27bbc89afca0463650ac9bde68ebe06&backurl=https%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3D%25E4%25B8%25AD%25E5%259B%25BD&logid=8566586814948436558&signature=d57f08acfd52c909965cd6d2edde421b×tamp=1581073954
# 查看响应头部字符编码
print(response.encoding)
ISO-8859-1
# 查看响应码
print(response.status_code)
200
发送Post请求
最基本的POST请求可以使用post方法
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36"}
params = {'wd': '美术'}
response = requests.post("http://www.baidu.com/", params=params, headers=headers)
with open('baidu.html', 'w',encoding='utf-8') as f:
f.write(response.content.decode('utf-8'))
# 注意:write方法只能写入字符串,content是bytes类型,所以要进行解码
使用代理
使用requests添加代理也非常简单,只要在请求的方法中(比如get或者post)传递proxies参数就可以了
url = "http://httpbin.org/get"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36',
}
proxy = {
'http': '171.14.209.180:27829'
}
resp = requests.get(url,headers=headers,proxies=proxy)
with open('123.html','w',encoding='utf-8') as fp:
fp.write(resp.text)
cookie
如果在一个响应中包含了cookie,那么可以利用cookies属性拿到这个返回的cookie值
url = "http://www.renren.com/PLogin.do"
data = {"email":"970138074@qq.com",'password':"pythonspider"}
resp = requests.get('http://www.baidu.com/')
print(resp.cookies)
print(resp.cookies.get_dict())
<RequestsCookieJar[<Cookie BDORZ=27315 for .baidu.com/>]>
{'BDORZ': '27315'}
Session
之前使用urllib库,是可以使用opener发送多个请求,多个请求之间是可以共享cookie的。那么如果使用requests,也要达到共享cookie的目的,那么可以使用requests库给我们提供的session对象。注意,这里的session不是web开发中的那个session,这个地方只是一个会话的对象而已。还是以登录人人网为例,使用requests来实现。
url = "http://www.renren.com/PLogin.do"
data = {"email":"970",'password':"python"}
headers = {
'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36"
}
# 登录
session = requests.session()
session.post(url,data=data,headers=headers)
resp = session.get('http://www.renren.com/880151247/profile')
处理不信任的SSL证书
对于那些已经被信任的SSL整数的网站,比如https://www.baidu.com/,那么使用requests直接就可以正常的返回响应
# resp = requests.get('http://www.12306.cn/mormhweb/',verify=False)
# print(resp.content.decode('utf-8'))
来源:CSDN
作者:GQ_on the way
链接:https://blog.csdn.net/qq_41506882/article/details/104215202