1. 程式人生 > >python3自動獲取網站的cookie

python3自動獲取網站的cookie

適合自動獲取ip控制的網站的cookie

import requests

session = requests.Session()
headers = {'Accept': 'text/html, application/xhtml+xml, image/jxr, */*',
           'Accept-Language': 'zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3',
           'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
', 'Accept-Encoding': 'gzip, deflate', 'Host': 'www.xxx.com', 'Connection': 'Keep-Alive', } def refreshCookie(): url='http://www.xxx.com/' res = requests.get(url) print(res.cookies) session.cookies = res.cookies return res.cookies cookieJar
=refreshCookie() cookies=cookieJar.get_dict() #cookies={'JSESSIONID': '83215BB45975A0795156209124F0CC54'} print(cookies)

請求的時候,使用以下程式碼:

r=session.get(url, headers=headers,cookies=cookies,verify=False)
r.raise_for_status()    # 如果響應狀態碼不是 200,就主動丟擲異常