第一种获取Cookies的方法
# 第一次获取cookies
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
'Referer': 'http://jkjmjzs.jinke.com/weixin/wxLogin.aspx'
}
url = 'http://jkjmjzs.jinke.com/weixin/wxLogin.aspx'
response = requests.get(url)
cookies = response.cookies.get_dict()
print(cookies)
# 第二次获取cookies
url = 'http://jkjmjzs.jinke.com/weixin/wxAction2.ashx?t=LD&rand=%s' % random.random()
payloads = {
'LoginID': 'username',
'Password': 'password',
'RememberPwd': 0
}
response = requests.post(url, data = payloads, headers = headers, cookies = cookies)
cookies.update(response.cookies.get_dict())
print(cookies)
第二种获取Cookies的方法
from urllib import request
from http import cookiejar
#跳过SSL验证证书
import ssl
#设置忽略SSL验证
ssl._create_default_https_context = ssl._create_unverified_context
if __name__ == '__main__':
#声明一个CookieJar对象实例来保存cookie
cookie = cookiejar.CookieJar()
#利用urllib.request库的HTTPCookieProcessor对象来创建cookie处理器,也就CookieHandler
handler=request.HTTPCookieProcessor(cookie)
#通过CookieHandler创建opener
opener = request.build_opener(handler)
#此处的open方法打开网页
response = opener.open('http://www.baidu.com')
#打印cookie信息
for item in cookie:
print('Name = %s' % item.name)
print('Value = %s' % item.value)
第三种获取Cookies的方法
def getCookie():
url = "https://passport.bilibili.com/login"
Hostreferer = {
'accept':'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.90 Safari/537.36'
}
#urllib或requests在打开https站点是会验证证书。 简单的处理办法是在get方法中加入verify参数,并设为False
html = requests.get(url, headers=Hostreferer,verify=False)
#获取cookie:DZSW_WSYYT_SESSIONID
if html.status_code == 200:
print(html.cookies)
for cookie in html.cookies:
print(cookie)