时间:2020-10-06来源:www.pcxitongcheng.com作者:电脑系统城
方案一:
利用selenium+phantomjs无界面浏览器的形式访问网站,再获取cookie值:
?| 1 2 3 4 5 6 7 8 9 10 |
from selenium import webdriver driver=webdriver.PhantomJS()url="https://et.xiamenair.com/xiamenair/book/findFlights.action?lang=zh&tripType=0&queryFlightInfo=XMN,PEK,2018-01-15"driver.get(url)# 获取cookie列表cookie_list=driver.get_cookies()# 格式化打印cookiefor cookie in cookie_list: cookie_dict[cookie['name']]=cookie['value'] |
方案二:
利用cookielib库获取:
(1)Python2
?| 1 2 3 4 5 6 7 8 9 |
import cookielibimport urllib2Url = "https://et.xiamenair.com/xiamenair/book/findFlights.action?lang=zh&tripType=0&queryFlightInfo=XMN,PEK,2018-01-15"cj = cookielib.CookieJar()opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))urllib2.install_opener(opener)resp = urllib2.urlopen(Url)for index, cookie in enumerate(cj): print '[',index, ']',cookie |
(2)Python3
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
from urllib import requestfrom http import cookiejar#跳过SSL验证证书import ssl#设置忽略SSL验证ssl._create_default_https_context = ssl._create_unverified_contextif __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) |
方案三:
利用requests库获取:
Python3
?| 1 2 3 4 5 6 7 8 9 10 11 12 13 |
def getCookie(): url = "****" Hostreferer = { #'Host':'***', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 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) |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
2023-03-17
python flask项目打包成docker镜像发布的过程2023-03-17
python调试模块ipdb详解2023-03-17
python使用openai生成图像的超详细教程python cron定时任务触发接口自动化巡检 apscheduler报错:Run time of job …… next run at: ……)” was missed by misfire_grace_time参数 找到任务超时的根本原因...
2023-03-15