python爬虫保存cookies时重要的两个参数(ignore_discard和ignore_expires)的作用
2023-09-27 14:27:48 时间
代码如下:(由于临时做的实例采用登录云打码平台的cookies)
import requests
from lxml.html import etree
from fake_useragent import UserAgent
from http.cookiejar import LWPCookieJar
from ydm import shibie
session = requests.Session()
session.cookies = LWPCookieJar(filename='ydm.txt')
try:
session.cookies.load('ydm.txt', ignore_discard=True, ignore_expires=True)
print('本地cookie加载完成')
except Exception as e:
print('')
ua = UserAgent()
headers = {
'headers': ua.random,
}
def get_captcha_image():
content = session.get('http://www.yundama.com/index/captcha', headers=headers).content
with open('captcha.jpg', 'wb') as f:
f.write(content)
def login():
get_captcha_image()
# 验证码保存到本地以后,对验证码进行识别
result = shibie('captcha.jpg')
print('识别结果:', result)
response = session.get('http://www.yundama.com/index/login?username=gaohairui&password=gao12345&utype=1&vcode={}'.format(result))
print(response.text)
session.cookies.save(ignore_discard=True, ignore_expires=True)
if __name__ == '__main__':
login()
不难看出最后的保存的两个参数:ignore_discard和ignore_expires
session.cookies.save(ignore_discard=True, ignore_expires=True)
两个参数的作用:
官方的解释:
ignore_discard: save even cookies set to be discarded.
ignore_expires: save even cookies that have expired.The file is overwritten if it already exists
不难看出两个参数的实际作用是:ignore_discard的意思是即使cookies将被丢弃也将它保存下来,ignore_expires的意思 是如果cookies已经过期也将它保存并且文件已存在时将覆盖,在这里,我们将这两个全部设置为True。运行之后,cookies 将被保存到ydm.txt文件中。
查看一下内容:
相关文章
- 【你评论,我送书】Python的爬虫基础知识
- 浅识XPath(熟练掌握XPath的语法)【python爬虫入门进阶】(03)
- 【Python开发】【神经网络与深度学习】网络爬虫之python实现
- 【Python开发】【神经网络与深度学习】网络爬虫之python实现
- 两种曲线点抽稀算法-Python实现 附代码
- Python爬虫爬取知乎小结
- 10个高效的Python爬虫框架
- LZ77压缩算法编码Python实现原理图解
- Python网络编程中的select 和 poll I/O复用的简单使用
- python 3.6.1 安装scrapy踩坑之旅
- python爬虫之selenium--加载浏览器配置
- 利用Python爬虫技术的一些骚操作
- 学会Python爬虫能赚大钱吗?Python爬虫赚钱渠道分享
- Python为什么成为人工智能的首选语言
- Python类、模块、包的区别
- Python 爬取网页中JavaScript动态添加的内容(一)
- Python爬虫:scrapy-splash的请求头和代理参数设置
- Python爬虫:scrapy框架请求参数meta、headers、cookies一探究竟
- Python爬虫:splash的安装与简单示例
- python基础之语句结束
- Python爬虫之三种网页抓取方法性能比较
- mac 在终端使用命令行启动脚本,无法使用自己安装的python去执行脚本问题 含 (which python 查看python解析器位置)
- 你不能错过的【Python爬虫】测试2(完整源代码+架构+结果)
- 爬虫-Python爬虫常用库
- Python 有序排列permutations,无序组合combinations,阶乘factorial函数
- PyQt(Python+Qt)学习随笔:gridLayout的layoutHorizontalSpacing和layoutVerticalSpacing属性
- 为什么Python中称__lt__、__gt__等为“富比较”方法
- Python爬虫学习系列教程-----------爬虫系列 你值的收藏
- 超牛逼!Python爬虫学习的完整路线推荐
- Python理解力是什么?为什么要使用它们?
- 7个Python特殊技巧,助力你的数据分析工作之路
- 在家靠python爬虫兼职月入3w+:成年人的世界,钱是底气!
- python爬虫入门,10分钟就够了,这可能是我见过最简单的基础教学
- python爬虫