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"))
更多方法檢視中文文檔= 。=