python 模拟登陆新浪微博 能识别验证码

#!/usr/bin/python
#coding=utf8
import urllib
import urllib2
import cookielib
import base64
import re
import json
import hashlib
import rsa  
import binascii
import sys

cj = cookielib.LWPCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)

postdata = {
    'entry': 'weibo',
    'gateway': '1',
    'from': '',
    'savestate': '7',
    'userticket': '1',
    'vsnf': '1',
    'su': '',
    'pwencode':'rsa2',
    'sr':'1366*768',
    'servertime': '',
    'nonce': '',
    'sp': '',
    'prelt':'26',
    'encoding': 'UTF-8',
    'pagerefer':'',
    'url': 'http://weibo.com/ajaxlogin.php?framelogin=1&callback=parent.sinaSSOController.feedBackUrlCallBack%27',
    'returntype':'META'
}

def get_servertime(username):
    url = 'http://login.sina.com.cn/sso/prelogin.php?entry=weibo&callback=sinaSSOController.preloginCallBack&su='+str(username)+'&rsakt=mod&checkpin=1&client=ssologin.js(v1.4.18)&_=1440837636897'
    data = urllib2.urlopen(url).read()
    print data;
    p = re.compile('\((.*)\)')
    try:
        json_data = p.search(data).group(1)
        data = json.loads(json_data)
        servertime = str(data['servertime'])
        nonce = data['nonce']
        showpin=data['showpin']
    pcid=data['pcid']
        publickey=data['pubkey']
    rsakv=data['rsakv']
    print data
        return servertime, nonce,showpin,pcid,publickey,rsakv
    except:
        print 'Get severtime error!'
        return None

def get_pwd(pwd, servertime, nonce,pubkey):
    rsaPublickey =int(pubkey, 16)
    key = rsa.PublicKey(rsaPublickey, 65537)
    message = str(servertime) + '\t' + str(nonce) + '\n' + str(pwd)
    passwd = rsa.encrypt(message, key)
    passwd = binascii.b2a_hex(passwd)
    return passwd

def get_user(username):
    username_ = urllib.quote(username)
    username = base64.encodestring(username_)[:-1]
    return username

def login():
    username = '***********'
    pwd = '************'
    username=get_user(username)
    try:
        servertime, nonce,showpin,pcid,publickey,rsakv= get_servertime(username)
    except:
        return
    global postdata
    if showpin == 1:
    url='http://www.abc.com/get_yzm/index/'+str(pcid)

    # 将pcid 传给一个能识别图片验证码的程序处理,处理完成后返回验证码
    code=urllib2.urlopen(url).read()
    postdata['pcid']=pcid
    postdata['door']=code
       
    postdata['servertime'] = servertime
    postdata['nonce'] =nonce
    postdata['su'] = username
    postdata['sp'] = get_pwd(pwd, servertime, nonce,publickey)
    postdata['rsakv']=rsakv;
    postdata = urllib.urlencode(postdata)
    headers = {'User-Agent':'Mozilla/5.0 (X11; Linux i686; rv:8.0) Gecko/20100101 Firefox/8.0'}
    url = 'http://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.18)'
    req = urllib2.Request(
        url = url,
        data = postdata,
        headers = headers
    )
    result = urllib2.urlopen(req)
    text = result.read()
    #print text.decode('gbk')
    p = re.compile('location\.replace\(\'(.*?)\'\)')
    try:
        login_url = p.search(text).group(1)
        print login_url
        result=urllib2.urlopen(login_url)
        for index, cookie in enumerate(cj):
            print '[',index, ']',cookie;
        print "susses"
    except:
        print 'Login error!'

login()

你可能感兴趣的:(python 模拟登陆新浪微博 能识别验证码)