loginurl = 'https://login.sina.com.cn/sso/qrcode/image?entry=weibo&size=180&callback=STK_{}'
texturl = session.get(loginurl.format((time.time() * 1000), headers=headers)).text
print(texturl)
#输出texturl = window.STK_16149…… && STK_1614992087527.347({"retcode":20000000,"msg":"succ","data":{"qrid":"2MjNgQt……","image":"\……"}});
session = requests.session()
loginurl = 'https://login.sina.com.cn/sso/qrcode/image?entry=weibo&size=180&callback=STK_{}'
texturl = session.get(loginurl.format((time.time() * 1000), headers=headers)).text
#通过筛选获得主要参数
xx = re.search("window.STK_\d+.\d+ && STK_\d+.\d+\(?", texturl)
x = json.loads(texturl.strip().lstrip(xx.group()).rstrip(");"))
qrid = x['data']['qrid']
image = x['data']['image']
imageurl = session.get('https:' + image, headers=headers).content
t = showpng(imageurl)
t.start()
qridurl = 'https://login.sina.com.cn/sso/qrcode/check?entry=weibo&qrid={}&callback=STK_{}'
qrid和callback
while 1:
dateurl = session.get(qridurl.format(qrid,(time.time() * 1000), headers=headers)).text
#输出dateurl = window.STK_1614995146615.8608 && STK_1614995146615.8608({"retcode":50114001,"msg":"\u672a\u4f7f\u7528","data":null});
xx = re.search("window.STK_\d+.\d+ && STK_\d+.\d+\(?", dateurl)
x = json.loads(dateurl.strip().lstrip(xx.group()).rstrip(");"))
retcode = x['retcode']
if '50114001' in str(retcode):
print('二维码未失效,请扫码!')
elif '50114002' in str(retcode):
print('已扫码,请确认!')
elif '50114004' in str(retcode):
print('二维码已失效,请重新运行!')
elif '20000000' in str(retcode):
print('已确认,登录成功!')
break
else:
print('其他情况',retcode)
time.sleep(5)
50114001:二维码未扫描状态
50114002:二维码已扫描未确认状态
20000000:二维码已确认状态
50114004:二维码已失效
window.STK_161498904169123 && STK_161498904169123({“retcode”:20000000,“msg”:“succ”,“data”:{“alt”:“ALT-NjE0Njc……”}});
alturl = 'https://login.sina.com.cn/sso/login.php?entry=weibo&returntype=TEXT&crossdomain=1&cdult=3&domain=weibo.com&alt=ALT-NjE0Njc4MTM……&savestate=30&callback=STK_161498904169125'
name | value |
---|---|
entry | |
returntype | TEXT |
crossdomain | 1 |
cdult | 3 |
domain | weibo.com |
alt | ALT-NjE0Njc4M…… |
savestate | 30 |
callback | STK_161498904169125 |
alt = x['data']['alt']
alturl = 'https://login.sina.com.cn/sso/login.php?entry=weibo&returntype=TEXT&crossdomain=1&cdult=3&domain=weibo.com&alt={}&savestate=30&callback=STK_{}'.format(alt,int(time.time() * 100000))
crossDomainUrlList = session.get(alturl,headers=headers).text
print(crossDomainUrlList)
#输出crossDomainUrlList = STK_161500519933534({"retcode":"0","uid":"61……","nick":"\u7528……","crossDomainUrlList":["https:\/\/……"]});
1.https://passport.9797……
2.https://passport.krco……
3.https://passport.weib……
4.https://passport.weibo.com/wbsso……
alturl = 'https://login.sina.com.cn/sso/login.php?entry=weibo&returntype=TEXT&crossdomain=1&cdult=3&domain=weibo.com&alt={}&savestate=30&callback=STK_{}'.format(alt,int(time.time() * 100000))
crossDomainUrl = session.get(alturl,headers=headers).text
pp = re.search("STK_\d+\(?", crossDomainUrl)
p = json.loads(crossDomainUrl.strip().lstrip(pp.group()).rstrip(");"))
crossDomainUrlList = p['crossDomainUrlList']
session.get(crossDomainUrlList[0], headers=headers)
session.get(crossDomainUrlList[1]+'&action=login', headers=headers)
session.get(crossDomainUrlList[2], headers=headers)
session.get(crossDomainUrlList[3], headers=headers)
print(session.cookies)
# -*- coding: utf-8 -*-
import json
import re
import agent
from threading import Thread
import time
import requests
from io import BytesIO
import http.cookiejar as cookielib
from PIL import Image
import os
requests.packages.urllib3.disable_warnings()
headers = {
'User-Agent': agent.get_user_agents(), 'Referer': "https://weibo.com/"}
class showpng(Thread):
def __init__(self, data):
Thread.__init__(self)
self.data = data
def run(self):
img = Image.open(BytesIO(self.data))
img.show()
def islogin(session):
try:
session.cookies.load(ignore_discard=True)
except Exception:
pass
loginurl = session.get("https://account.weibo.com/set/aj/iframe/schoollist?province=11&city=&type=1&_t=0&__rnd={}".format(int(time.time() * 1000)), headers=headers).json()['code']
if loginurl == '100000':
print('Cookies值有效,无需扫码登录!')
return session, True
else:
print('Cookies值已经失效,请重新扫码登录!')
return session, False
def wblogin():
if not os.path.exists('wbcookies.txt'):
with open("wbcookies.txt", 'w') as f:
f.write("")
session = requests.session()
session.cookies = cookielib.LWPCookieJar(filename='wbcookies.txt')
session, status = islogin(session)
if not status:
loginurl = 'https://login.sina.com.cn/sso/qrcode/image?entry=weibo&size=180&callback=STK_{}'
texturl = session.get(loginurl.format(int(time.time() * 1000), headers=headers)).text
xx = re.search("window.STK_\d+.\d+ && STK_\d+.\d+\(?", texturl)
x = json.loads(texturl.strip().lstrip(xx.group()).rstrip(");"))
qrid = x['data']['qrid']
image = x['data']['image']
imageurl = session.get('https:' + image, headers=headers).content
t = showpng(imageurl)
t.start()
qridurl = 'https://login.sina.com.cn/sso/qrcode/check?entry=weibo&qrid={}&callback=STK_{}'
while 1:
dateurl = session.get(qridurl.format(qrid, int(time.time() * 1000), headers=headers)).text
xx = re.search("window.STK_\d+.\d+ && STK_\d+.\d+\(?", dateurl)
x = json.loads(dateurl.strip().lstrip(xx.group()).rstrip(");"))
retcode = x['retcode']
if '50114001' in str(retcode):
print('二维码未失效,请扫码!')
elif '50114002' in str(retcode):
print('已扫码,请确认!')
elif '50114004' in str(retcode):
print('二维码已失效,请重新运行!')
elif '20000000' in str(retcode):
alt = x['data']['alt']
alturl = 'https://login.sina.com.cn/sso/login.php?entry=weibo&returntype=TEXT&crossdomain=1&cdult=3&domain=weibo.com&alt={}&savestate=30&callback=STK_{}'.format(
alt, int(time.time() * 100000))
crossDomainUrl = session.get(alturl, headers=headers).text
pp = re.search("STK_\d+\(?", crossDomainUrl)
p = json.loads(crossDomainUrl.strip().lstrip(pp.group()).rstrip(");"))
crossDomainUrlList = p['crossDomainUrlList']
session.get(crossDomainUrlList[0], headers=headers)
session.get(crossDomainUrlList[1] + '&action=login', headers=headers)
session.get(crossDomainUrlList[2], headers=headers)
session.get(crossDomainUrlList[3], headers=headers)
print('已确认,登录成功!')
break
else:
print('其他情况', retcode)
time.sleep(5)
session.cookies.save()
return session
if __name__ == '__main__':
wblogin()
- 后期小编将开设登录后批量采集各平台数据(点赞、播放量、评论、图片、视频、音乐等)专栏文章!记得关注哟!
- 如果文章能帮到您,愿意给小编点个 赞 吗,么么哒~ (●’◡’●)