python实现二维码扫码自动登录淘宝
作者:Gary Zhang 时间:2022-09-22 07:18:02
一个小项目自动登录淘宝联盟抓取数据,由于之前在Github上看过类似用Python写的代码因此选择用Python来写,第一次用Python正式写程序还是被其“简单”所震撼,当然用的时候还是对其(2.7版)编码、迁移环境等问题所困扰,还好后来都解决了。
言归正传,抓取淘宝联盟的数据首先要解决的就是登录的问题,之前一般会碰到验证码的困扰,现在支持二维码扫码登录反而简单了,以下是登录的Python代码,主要是获取二维码打印,然后不断的检查扫码状态,如果过期了重新请求二维码(主要看逻辑,由于有些通用方法做了封装所以不保证能直接执行)
def getQRCode(enableCmdQR):
payload = {'_ksTS': str(time.time()), 'from': 'alimama'}
qrCodeObj = utils.fetchAPI('https://qrlogin.taobao.com/qrcodelogin/generateQRCode4Login.do', payload,
"json", None, True, True)
print(qrCodeObj)
utils.printQRCode('http:' + qrCodeObj['url'], enableCmdQR)
lgToken = qrCodeObj['lgToken']
return lgToken
def login(enableCmdQR=False):
lgToken = getQRCode(enableCmdQR)
code = 0
successLoginURL = ""
while code != 10006:
payload = {'lgToken': lgToken,
'defaulturl': 'http%3A%2F%2Flogin.taobao.com%2Fmember%2Ftaobaoke%2Flogin.htm%3Fis_login%3D1&_ksTS=' + str(
time.time())}
rObj = utils.fetchAPI('https://qrlogin.taobao.com/qrcodelogin/qrcodeLoginCheck.do', payload, "json", True,
False)
code = int(rObj['code'])
if 10000 == code:
# print("请扫描二维码登录")
continue
elif 10001 == code:
print("已扫描二维码,请在确认登录")
elif 10004 == code:
print("已过期请重新扫描")
login()
elif 10006 == code:
successLoginURL = rObj["url"]
print("登录成功,正在跳转")
else:
print("未知错误,退出执行")
sys.exit(0)
time.sleep(5)
print "登录成功跳转:" + successLoginURL
r = utils.fetchAPI(successLoginURL, None, "raw", True, False, True)
utils.fetchAPI(r.headers['Location'], None, "raw", True, True, False)
解决登录问题接下去就要解决保存状态的问题,Python的Requests库非常强大,如果简单的话可以直接使用request.session来进行会话操作,但由于项目中的很多操作是异步的因此需要解决cookie的存储和读取,使用pickel进行对像的序列化和反序列化。其中保存cookie默认用增量的方式进行更新
def save_cookies(cookies, overWrite=False):
try:
currentCookie = requests.utils.dict_from_cookiejar(cookies)
if len(currentCookie) < 1:
return
oldCookie = requests.utils.dict_from_cookiejar(load_cookies())
with open(config.COOKIE_FILE, 'w') as f:
if not overWrite:
cookieDict = dict(oldCookie, **currentCookie)
else:
cookieDict = requests.utils.dict_from_cookiejar(cookies)
pickle.dump(cookieDict, f)
print 'Saved cookie'
print cookieDict
f.close()
except:
print 'Save cookies failed', sys.exc_info()[0]
sys.exit(99)
def load_cookies():
try:
with open(config.COOKIE_FILE, 'r') as f:
cookies = requests.utils.cookiejar_from_dict(pickle.load(f))
f.close()
except:
cookies = []
return cookies
封装好之后,在requests.Session请求时加载cookie并保存cookie
s = requests.Session()
# 统一请求API
def fetchAPI(url, params=None, resultFormat="text", isNeedCookie=True, allowRedirects=True, saveCookie=False,
method='GET'):
try:
cookies = load_cookies()
if 'POST' == method:
response = s.post(url, data=params, headers=config.Headers, cookies=cookies)
else:
response = s.get(url, params=params, headers=config.Headers, cookies=cookies,
allow_redirects=allowRedirects)
if "json" == resultFormat:
result = response.json()
elif "raw" == resultFormat:
result = response
else:
result = response.text
# if saveCookie:
# print 'save cookie:' + str(response.cookies)
save_cookies(response.cookies)
return result
except Exception, e:
print e
return False
这两步做好之后基本后续的请求就直接使用统一的API请求方法即可,效果也非常不错,运行效果截图:
当然还有一个问题未解决:如何在session过期之后如何自动重新申请(不确定淘定是否支持),由于淘宝是用统一登录而且是独立的服务因此通过浏览器自动刷新或者请求过程中不断去更新cookie都没有获得服务器方更新的票据,不知道这一块大家有没有可以提供的思路。
标签:python,二维码,自动登录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
django admin组件使用方法详解
2021-05-28 19:59:53
golang 跳出for循环操作
2024-04-30 10:04:00
python验证码识别的示例代码
2023-08-04 03:20:24
![](https://img.aspxhome.com/file/2023/0/107820_0s.png)
python中dict获取关键字与值的实现
2022-03-11 14:00:42
![](https://img.aspxhome.com/file/2023/8/103718_0s.png)
Python数据类型详解(三)元祖:tuple
2021-05-17 07:49:14
Python pyecharts绘制折线图详解
2021-05-22 17:42:49
![](https://img.aspxhome.com/file/2023/3/93623_0s.png)
Python3 导入上级目录中的模块实例
2023-09-01 02:25:20
利用go语言实现查找二叉树中的最大宽度
2024-05-28 15:22:31
![](https://img.aspxhome.com/file/2023/2/123892_0s.webp)
python 实现存储数据到txt和pdf文档及乱码问题的解决
2023-02-05 03:40:00
![](https://img.aspxhome.com/file/2023/8/134638_0s.jpg)
Django后端接收嵌套Json数据及解析详解
2021-04-24 20:11:38
![](https://img.aspxhome.com/file/2023/1/105711_0s.png)
python如何在循环引用中管理内存
2023-12-20 13:18:40
python xlsxwriter创建excel图表的方法
2021-01-12 03:16:10
![](https://img.aspxhome.com/file/2023/0/89180_0s.jpg)
Go语言学习之goroutine详解
2024-03-25 02:45:56
Django 反向生成url实例详解
2022-07-21 23:51:05
在VScode里面添加Python解释器的详细步骤
2021-12-04 16:03:11
![](https://img.aspxhome.com/file/2023/7/100817_0s.png)
Python使用pyecharts绘制世界地图,省级地图,城市地图实例详解
2022-08-08 06:37:22
![](https://img.aspxhome.com/file/2023/9/72119_0s.jpg)
oracle中commit之后进行数据回滚的方法
2024-01-25 01:00:53
mysql 左连接、右连接和内连接
2024-01-22 10:14:39
让文本框textarea自动适应内容的高度
2008-07-01 14:44:00
python tkinter实现学生信息管理系统
2021-11-01 18:46:16