色彩时光 | 记录程序员世界的点点滴滴

python模拟微博登陆


python模拟微博登陆


# -*- coding: utf-8 -*-

import re
import json
import base64
import binascii
import rsa
import requests
import urllib2
import urllib
import random
import os
import time
from config import WBCLIENT, USER_AGENT
from config import USER_NAME, PASSWD
from logger import logger

session = requests.session()
session.headers['User-Agent'] = USER_AGENT


def encrypt_passwd(passwd, pubkey, servertime, nonce):
    key = rsa.PublicKey(int(pubkey, 16), int('10001', 16))
    message = str(servertime) + '\t' + str(nonce) + '\n' + str(passwd)
    passwd = rsa.encrypt(message.encode('utf-8'), key)
    return binascii.b2a_hex(passwd)


def prelogin_parse(resp):
    p = re.compile('preloginCallBack\((.+)\)')
    data = json.loads(p.search(resp).group(1))
    return data


def prelogin():
    username = USER_NAME
    password = PASSWD
    url = ('http://login.sina.com.cn/sso/prelogin.php?'
           'entry=weibo&callback=sinaSSOController.preloginCallBack&rsakt=mod&checkpin=1&'
           'su={username}&_={timestamp}&client={client}'
           ).format(username=username, timestamp=int(time.time() * 1000), client=WBCLIENT)

    resp = urllib2.urlopen(url).read()
    return prelogin_parse(resp)


def process_verify_code(pcid):
    url = 'http://login.sina.com.cn/cgi/pin.php?r={randint}&s=0&p={pcid}'.format(
        randint=int(random.random() * 1e8), pcid=pcid)
    filename = 'pin.png'
    if os.path.isfile(filename):
        os.remove(filename)

    urllib.urlretrieve(url, filename)
    if os.path.isfile(filename):  # get verify code successfully
        #  display the code and require to input
        # from PIL import Image
        # import subprocess
        # proc = subprocess.Popen(['display', filename])
        code = raw_input('请输入验证码:')
        # os.remove(filename)
        # proc.kill()
        return dict(pcid=pcid, door=code)
    else:
        return dict()


def wblogin():
    # info = prelogin()
    # print info

    username = USER_NAME
    password = PASSWD
    resp = session.get(
        'http://login.sina.com.cn/sso/prelogin.php?'
        'entry=weibo&callback=sinaSSOController.preloginCallBack&'
        'su=%s&rsakt=mod&checkpin=1&client=%s' %
        (base64.b64encode(username.encode('utf-8')), WBCLIENT)
    )

    pre_login_str = re.match(r'[^{]+({.+?})', resp.text).group(1)
    pre_login = json.loads(pre_login_str)
    print pre_login
    print "--" * 21

    data = {
        'entry': 'weibo',
        'gateway': 1,
        'from': '',
        'savestate': 7,
        'userticket': 1,
        'ssosimplelogin': 1,
        'su': base64.b64encode(requests.utils.quote(username).encode('utf-8')),
        'service': 'miniblog',
        'servertime': pre_login['servertime'],
        'nonce': pre_login['nonce'],
        'vsnf': 1,
        'vsnval': '',
        'pwencode': 'rsa2',
        'sp': encrypt_passwd(password, pre_login['pubkey'],
                             pre_login['servertime'], pre_login['nonce']),
        'rsakv': pre_login['rsakv'],
        'encoding': 'UTF-8',
        'prelt': '53',
        'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.si'
               'naSSOController.feedBackUrlCallBack',
        'returntype': 'META'
    }

    if 'showpin' in pre_login and pre_login['showpin']:  # need to input verify code
        data.update(process_verify_code(pre_login['pcid']))

    login_url_list = 'http://login.sina.com.cn/sso/login.php?client=%s' % WBCLIENT
    resp = session.post(login_url_list, data=data)
    match_obj = re.search('replace\\(\'([^\']+)\'\\)', resp.text)
    if match_obj is None:
        logger.info('登录失败,请检查登录信息')
        return (session, None)

    login_url = match_obj.group(1)
    resp = session.get(login_url)
    login_str = login_str = re.search('\((\{.*\})\)', resp.text).group(1)
    login_info = json.loads(login_str)
    logger.info("login success:[%s]" % str(login_info))
    uniqueid = login_info["userinfo"]["uniqueid"]
    return (session, uniqueid)


if __name__ == '__main__':
    (http, uid) = wblogin()
    text = http.get('http://weibo.com/').text
    print(text)

新浪微博模拟登录分析(含验证码)

Python代码登录新浪微博并自动发微博,不使用SDK


您可能也对下面文章感兴趣:

Write a Comment


* Content (required) 10~500s

分类

热门标签

友情链接