Requests: 让 HTTP 服务人类。 Requests 允许你发送纯天然,植物饲养的 HTTP/1.1
请求,无需手工劳动。你不需要手动为 URL 添加查询字串,也不需要对 POST 数据进行表单编码。Keep-alive 和 HTTP
连接池的功能是 100% 自动化的,一切动力都来自于根植在 Requests 内部的urllib3。
中文文档 https://2.python-requests.org/zh_CN/latest/
获取源码github
git clone git://github.com/kennethreitz/requests.git
pip安装
pip install requests
发送请求
import requests
#请求头信息
headers = {"User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"}
#请求参数
param = {"data": "postparam"}
#发送请求 requests.post(),requests.put() ...
#response = requests.get("https://www.baidu.com")
response = requests.post("http://118.190.201.101:80/post", data=param, headers=headers)
#response = requests.get("http://118.190.201.101:80/get")
#响应状态码
print(response.status_code)
#响应内容
print(response.content.decode("utf-8"))
#cookie
print(response.cookies)
#响应头信息
print(response.headers)
cookie信息
response = requests.get("https://www.baidu.com")
for k, v in response.cookies.items():
print(k + ':' + v)
手动设置cookie模拟登陆
#登录想模拟登录的网站手动复制过来cookie,也可以添加自己想要的信息
cookie = "_zap=8f1bfe4a-3d3c-458b-9be1-57c7a8ec33f0; d_c0=\"AHCkoEe1CA-PTnqSsziPvLepAD3sWHFnh2o=|1551062430\";" \
" q_c1=bd23fb9aaeb9450bbd1291bd4d2ed27f|1553308445000|1553308445000; _ga=GA1.2.83809550.1594704344; _xsrf=P0BbG4ld01xi8yZ47RX4OU8dDSlzrxeZ;" \
" Hm_lvt_98beee57fd2ef70ccdd5ca52b9740c49=1599125165,1599557693,1599558409,1600060011; l_n_c=1;" \
" r_cap_id=\"ODNhNzQ0ZjQxN2U3NDZmMThlNjE2M2FkODNhZjhkZjk=|1600060016|2cb55d04b5c3ea87d690dfc5b31e25cd8b9e7877\";" \
" cap_id=\"Y2NiMzIxNTA3MzUwNDA2NWEwZTc2NzhmNTA4ZDI5ZmY=|1600060016|1ac346d45897b5c0b055e537953645a545c763bb\";" \
" l_cap_id=\"ZDhmMmMyMDQ2YzY3NDRhZGJmNmY2N2QzM2RjYTQwMjg=|1600060016|dd8831c1361df0c02d099869b00c3885c88a47ce\"; n_c=1;" \
" capsion_ticket=\"2|1:0|10:1600060030|14:capsion_ticket|44:ODJlNzVjYTE2OWQ0NDRhMzk0ZGM2ODg3YjQwYjA0YzA=|c242047639de52e6bb5a17c395c450b068048bc92da962c0354dc7bb8d76a6e4\";" \
" z_c0=\"2|1:0|10:1600060070|4:z_c0|92:Mi4xZUhRY0h3QUFBQUFBY0tTZ1I3VUlEeVlBQUFCZ0FsVk5wa2hNWUFCMzF2ZjVqODhoa0F6M05kQm5SdHNETzJnTVNR|f9d415b59dd3bd4e5f33126d9397742afa58743e9e9ebc1933730530d669440c\";" \
" tst=r; _gid=GA1.2.124303192.1600060078; Hm_lpvt_98beee57fd2ef70ccdd5ca52b9740c49=1600060410"
#将cookie放到headers发送请求
headers = {"cookie": cookie, "Host": "www.zhihu.com", "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36"}
response = requests.get("https://www.zhihu.com/", headers=headers)
print(response.content.decode("utf-8"))
IP代理
#IP代理
proxies = {"http": "61.135.186.243:80"}
#设置代理 请求超时时间 1s , timeout设为None 或者不设置会一直等待
response = requests.get("https://www.baidu.com", proxies=proxies, timeout=1)
print(response.content.decode("utf-8"))
SESSION (跨http请求保持某些参数,比如请求头信息)
#利用seesion
client = requests.Session()
response = client.get("https://www.zhihu.com/", headers=headers)
print(response.content.decode("utf-8"))
SSL证书
#SSL证书验证 verify默认为True会自动验证证书,设成False则不验证
response = requests.get('https://www.12306.cn/index/', verify=False)
print(response.content.decode("utf-8"))