Python全系列 教程
3567个小节阅读:5930.3k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
问题
不想手动复制cookie,太繁琐了!
解决方案
在再代码中执行登录操作,并保持Cookie不丢失
为了保持Cookie不丢失可以urllib.request.HTTPCookieProcessor
来扩展opener的功能
xxxxxxxxxx
from urllib.request import Request,build_opener
from fake_useragent import UserAgent
from urllib.parse import urlencode
from urllib.request import HTTPCookieProcessor
login_url ='https://www.kuaidaili.com/login/'
args = {
'username':'398707160@qq.com',
'passwd':'123456abc'
}
headers = {
'User-Agent':UserAgent().chrome
}
req = Request(login_url,headers= headers,data = urlencode(args).encode())
# 创建一个可以保存cookie的控制器对象
handler = HTTPCookieProcessor()
# 构造发送请求的对象
opener = build_opener(handler)
# 登录
resp = opener.open(req)
'''
-------------------------上面已经登录好----------------------------------
'''
index_url ='https://www.kuaidaili.com/usercenter/overview'
index_req = Request(index_url,headers =headers)
index_resp = opener.open(index_req)
print(index_resp.read().decode())
实时效果反馈
1. 关于爬虫,使用Cookie说法错误的?
A opener本身可以保存cookie,无需扩展
B urllib.request.HTTPCookieProcessor
是opender辅助对象
C urllib.request.HTTPCookieProcessor
可以保存cookie
D 为保持cookie,多次请求使用同一个opener
答案
1=>A