人人网

# 人人网的模拟登陆,配合验证码使用

#人人网携带验证码的模拟登陆
#1.识别验证码
import requests
from lxml import etree
from urllib import request
import http.client, mimetypes, urllib, json, time, requests
# 封装识别验证码图片的函数 
def getCodeText(filename,codetype):
    result = None
    username    = 'zhangc' 
    password    = 'zc1991'                            
    appid       = 7568                                     
    appkey      = 'bbb4575c2c541e5fa4fe932ce86774ef'    
    filename    = filename                     
    codetype    = codetype
    timeout     = 40                                 
    if (username == 'username'):
        print('请设置好相关参数再测试')
    else:
        yundama = YDMHttp(username, password, appid, appkey)
        uid = yundama.login();      
        balance = yundama.balance();      
        cid, result = yundama.decode(filename, codetype, timeout);
    return result
# 一个类  -----------------------------------------------------------  
class YDMHttp:
    apiurl = 'http://api.yundama.com/api.php'
    username = ''
    password = ''
    appid = ''
    appkey = ''

    def __init__(self, username, password, appid, appkey):
        self.username = username  
        self.password = password
        self.appid = str(appid)
        self.appkey = appkey

    def request(self, fields, files=[]):
        response = self.post_url(self.apiurl, fields, files)
        response = json.loads(response)
        return response
    
    def balance(self):
        data = {'method': 'balance', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['balance']
        else:
            return -9001
    
    def login(self):
        data = {'method': 'login', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey}
        response = self.request(data)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['uid']
        else:
            return -9001

    def upload(self, filename, codetype, timeout):
        data = {'method': 'upload', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'codetype': str(codetype), 'timeout': str(timeout)}
        file = {'file': filename}
        response = self.request(data, file)
        if (response):
            if (response['ret'] and response['ret'] < 0):
                return response['ret']
            else:
                return response['cid']
        else:
            return -9001

    def result(self, cid):
        data = {'method': 'result', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid)}
        response = self.request(data)
        return response and response['text'] or ''

    def decode(self, filename, codetype, timeout):
        cid = self.upload(filename, codetype, timeout)
        if (cid > 0):
            for i in range(0, timeout):
                result = self.result(cid)
                if (result != ''):
                    return cid, result
                else:
                    time.sleep(1)
            return -3003, ''
        else:
            return cid, ''

    def report(self, cid):
        data = {'method': 'report', 'username': self.username, 'password': self.password, 'appid': self.appid, 'appkey': self.appkey, 'cid': str(cid), 'flag': '0'}
        response = self.request(data)
        if (response):
            return response['ret']
        else:
            return -9001

    def post_url(self, url, fields, files=[]):
        for key in files:
            files[key] = open(files[key], 'rb');
        res = requests.post(url, files=files, data=fields)
        return res.text
# -------------------------------------------------------------------
url = 'http://www.renren.com/SysHome.do'
headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36',
    "Connection": "close"
}
page_text = requests.get(url=url,headers=headers,proxies={'http':'117.127.0.202:80'}).text
tree = etree.HTML(page_text)
code_img_url = tree.xpath('//*[@id="verifyPic_login"]/@src')[0]
request.urlretrieve(url=code_img_url,filename='./code.jpg')
# 使用打码平台识别验证码
code_text = getCodeText('./code.jpg',2004)
print(code_text)
# 模拟登陆
login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2019402143300'
data = {
    "email": "15851646900",
    "icode": code_text,
    "origURL": "http://www.renren.com/home",
    "domain": "renren.com",
    "key_id": "1",
    "captcha_type": "web_login",
    "password": "6222309eade4d7cfd41c0fc992aae8f13ec49a6afe582e672f0c3e287577502b",
    "rkey": "83825afc2a1e49b07bb3daf6e4f14059",
    "f": "http%3A%2F%2Fwww.renren.com%2F970438501"
}
response = requests.post(url=login_url,headers=headers,data=data,proxies={'http':'117.127.0.202:80'})
print(response.status_code)
page_t = response.text
print(page_t)

 

posted @ 2019-05-21 21:07  追风zz  阅读(922)  评论(0编辑  收藏  举报