python3模拟登陆新浪微博


原文:https://blog.csdn.net/songzhilian22/article/details/48396545

以下是使用python3改写的代码,具体分析过程请查阅我之前在CSDN上面写的博客。

import base64
import requests
import re
import rsa
import binascii
import json
from lxml import etree

def get_cookies(username, password, headers):
    '''
    :param username: 传入微博的登录名username
    :param password:  传入微博的登录名password
    :param headers:  模拟登陆需要到的headers
    :return: 返回成功登陆之后的cookies
    '''
    url = 'http://login.sina.com.cn/sso/prelogin.php?entry=sso&callback=sinaSSOController.preloginCallBack&su=%s&rsakt=mod&client=ssologin.js(v1.4.4)%' + username
    html = requests.get(url, headers=headers).text
    html = json.loads(re.findall('\(({.*?})\)', html, re.S)[0])

    rsaPublickey = int(html['pubkey'], 16)
    key = rsa.PublicKey(rsaPublickey, 65537) #创建公钥
    message = str(html['servertime']) + '\t' + str(html['nonce']) + '\n' + str(password) #拼接明文js加密文件中得到
    passwd = rsa.encrypt(message.encode(), key) #加密
    passwd = binascii.b2a_hex(passwd) #将加密信息转换为16进制。

    # 登陆页面
    login_url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.4)'
    # 需要提交的表单
    data = {'entry': 'weibo',
        'gateway': '1',
        'from': '',
        'savestate': '7',
        'userticket': '1',
        'ssosimplelogin': '1',
        'vsnf': '1',
        'vsnval': '',
        'su': base64.b64encode(username.encode()),  #加密用户名
        'service': 'miniblog',
        'servertime': html['servertime'],
        'nonce': html['nonce'],
        'pwencode': 'rsa2',
        'sp': passwd,
        'encoding': 'UTF-8',
        'prelt': '115',
        'rsakv' : html['rsakv'],
        'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack',
        'returntype': 'META'}
    html = requests.post(login_url, data=data, headers=headers).text
    urlnew = re.findall('location.replace\(\'(.*?)\'',html,re.S)[0]
    return requests.get(urlnew).cookies #发送get请求并保存cookies

def get_uid(cookies, headers):
    '''
    :param cookies: 登陆微博后的cookies
    :param headers: 模拟登陆需要到的headers
    :return: 返回新浪微博用户的UID,用于访问微博其他用户信息
    '''
    headers['Host'] = 'weibo.com'
    headers['Referer'] = 'https://weibo.com/'
    MyUID_html = requests.get(url = 'http://weibo.com/', cookies=cookies, headers=headers).text #用get请求加入cookies参数登陆微博主页
    UID = {'MyUID':{
        'uid': re.findall("\$CONFIG\['uid'\]='(.*?)';", MyUID_html, re.S)[0],  #获取我的uid
        'nick': re.findall("\$CONFIG\['nick'\]='(.*?)';", MyUID_html, re.S)[0] #获取我的用户名
    }}
    FollowUID_html = requests.get(url = 'http://weibo.com/' + UID['MyUID']['uid'] + '/follow', cookies=cookies, headers=headers).text
    FollowUID_html = json.loads(re.findall('<script>FM.view\((.*?)\)</script>', FollowUID_html, re.S)[-1])['html']
    FollowUID = []
    for i in etree.HTML(FollowUID_html).xpath('//a[@node-type=\"screen_name\"]'):
        FollowUID.append({
            'uid': i.xpath('@usercard')[0][3:],
            'nick': i.xpath('text()')[0]
        })
    UID.update({'FollowUID': FollowUID}) # 添加关注用户UID到字典
    return UID

if __name__ == '__main__':
    username = '' #登录名
    password = '' #密码

    headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36'}
    cookies = get_cookies(username, password, headers)   #获取登陆后的cookies
    uid = get_uid(cookies,headers)
    print(uid)

从截图可以看到 ,已经成功获取了我关注的用户的UID和用户名信息,说明已经成功模拟登陆了新浪微博,并获取到登陆后的cookies。接下来你可以使用这部分uid和cookies去访问这些用户的微博了:


 评论