python中3种获取cookie解决方案

简介: python中3种获取cookie解决方案

方案一:

利用selenium+phantomjs无界面浏览器的形式访问网站,再获取cookie值:

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()
# 格式化打印cookie
for cookie in cookie_list:
    cookie_dict[cookie['name']]=cookie['value']

方案二:

利用cookielib库获取:

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)

方案三:

利用requests库获取:

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)
相关文章
|
7月前
|
Linux 计算机视觉 C++
【解决方案】Building wheel for opencv-python:安装卡顿的原因与解决方案
当你安装OpenCV时,命令行停在Building wheel for opencv-python (PEP 517) ... -似乎卡住了。这并非程序假死,而是其编译耗时巨大。本文将揭示原因,并提供优化安装体验的实用方法。
927 88
|
7月前
|
Web App开发 数据安全/隐私保护 Python
万能ck提取登录软件,京东贴吧淘宝拼多多cookie提取工具,python框架分享
这个框架使用了Selenium进行浏览器自动化操作,包含了京东和淘宝的登录示例。代码展示了如
|
8月前
|
数据采集 存储 Web App开发
Python爬虫技巧:设置Cookie永不超时的详细指南
Python爬虫技巧:设置Cookie永不超时的详细指南
|
5月前
|
异构计算 Python
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
460 1
|
5月前
|
人工智能 Shell Python
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
ERROR: pip’s dependency resolver does not currently take into 报错-Python项目依赖冲突的解决方案-优雅草优雅草卓伊凡
278 0
|
7月前
|
数据采集 存储 数据库
Python爬虫开发:Cookie池与定期清除的代码实现
Python爬虫开发:Cookie池与定期清除的代码实现
|
7月前
|
存储 数据库 数据安全/隐私保护
抖音ck提取工具,快手小红书微博哔哩哔哩cookie提取登录软件,python框架
这个框架提供了完整的Cookie提取功能,支持抖音、快手、小红书、微博和哔哩哔哩平台。主要特点包括
|
10月前
|
并行计算 Python 容器
uv找不到Python头文件的解决方案
最近在微调LLM的时候,我发现使用uv构建的环境,有时候会找不到Python.h,导致一些库报错,如`fatal error: Python.h: No such file or directory`。通过设置`python-preference`可以解决。
755 35
|
7月前
|
数据采集 Web App开发 iOS开发
解决Python爬虫访问HTTPS资源时Cookie超时问题
解决Python爬虫访问HTTPS资源时Cookie超时问题
|
9月前
|
数据采集 存储 NoSQL
Python爬虫Cookie管理最佳实践:存储、清理与轮换
Python爬虫Cookie管理最佳实践:存储、清理与轮换

推荐镜像

更多