python爬虫学习(十七)人人网模拟登录

注意事项,页面产生跳转html保存下来的是一串字符
验证码验证类型记得改
可以用页面返回值200验证登录成功与否

import requests
from lxml import etree
from codeClass import YDMHttp


#封装打码平台代码
path = 'code.jpg'
def getCodeText(imgPath,codeType):
    # 用户名
    username = 'yxxxxxxxxxxxxxx0'

    # 密码
    password = 'yxxxxxxxxxx0'

    # 软件ID,开发者分成必要参数。登录开发者后台【我的软件】获得!
    appid = 9xxx2

    # 软件密钥,开发者分成必要参数。登录开发者后台【我的软件】获得!
    appkey = '3dfbf90c1xxxxxxxxxx0d982ffb1c93'

    # 图片文件
    filename = imgPath

    # 验证码类型,# 例:1004表示4位字母数字,不同类型收费不同。请准确填写,否则影响识别率。在此查询所有类型 http://www.yundama.com/price.html
    codetype = codeType

    # 超时时间,秒
    timeout = 20
    result = None
    # 检查
    if (username == 'username'):
        print('请设置好相关参数再测试')
    else:
        # 初始化
        yundama = YDMHttp(username, password, appid, appkey)

        # 登陆云打码
        uid = yundama.login();
        print('uid: %s' % uid)

        # 查询余额
        balance = yundama.balance();
        print('余额: %s' % balance)

        # 开始识别,图片路径,验证码类型ID,超时时间(秒),识别结果
        cid, result = yundama.decode(filename, codetype, timeout);
        print('cid: %s, result: %s' % (cid, result))
    return result


#1、对验证码图片进行捕获和识别
headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36'
    }
url = "http://www.renren.com/SysHome.do"


page_text=requests.get(url=url,headers=headers).text
tree = etree.HTML(page_text)
code_img_src = tree.xpath('//*[@id="verifyPic_login"]/@src')[0]
code_img_data = requests.get(url=code_img_src,headers=headers).content
with open('./code.jpg','wb') as fp:
    fp.write(code_img_data)

#使用云打码对验证码进行识别
result = getCodeText('code.jpg',5000)
print(result)

#post请求的发送(模拟登录)
login_url = 'http://www.renren.com/ajaxLogin/login?1=1&uniqueTimestamp=2020021132191'
data={
    'email': '[email protected]',
    'icode':result,
    'origURL': 'http://www.renren.com/home',
    'domain': 'renren.com',
    'key_id': '1',
    'captcha_type': 'web_login',
    'password': '7d2xxxxxxxxxxxxxxx92f3b0',
    'rkey': '8a9fbb66f5xxxxxxxxxxxxx111173b047',
    'f': 'http%3xxxxxxxxxxxxxxxx67891%2Fprofile'
}
response=requests.post(url = login_url,headers=headers,data=data)
print(response.status_code)
login_page_text = response.text
with open('renren.html','w',encoding='utf-8')as fp:
    fp.write(login_page_text)
发布了23 篇原创文章 · 获赞 0 · 访问量 663

猜你喜欢

转载自blog.csdn.net/haimian_baba/article/details/104060296