python3自动获取网站的cookie

适合自动获取ip控制的网站的cookie

import requests

session = requests.Session()
headers_xxx = {'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 LoginCookie(url):
    global session
    session.get(url, headers=headers_xxx)
    print(session.cookies)
    return session.cookies.get_dict()

请求的时候,使用以下代码:

cookies_xxx=LoginCookie('http://www.xxx.com/')
r=session.get(url, headers=headers_xxx,cookies=cookies_xxx,verify=False,timeout=30) r.raise_for_status() # 如果响应状态码不是 200,就主动抛出异常

 

posted @ 2018-12-22 21:59  重积德  阅读(4970)  评论(1编辑  收藏  举报