Python爬虫学习笔记一:模拟登录(人工输入简单验证码)

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

import requests, base64, re
from PIL import Image
from io import BytesIO

def logon():
    loginUrl = 'http://############/login.do?method=enterbs'  #登陆提交地址
    imgUrl = 'http://############/form/digitalverify.jsp'     #验证码获取地址
    s = requests.session() #建立一个会话
    res = s.get(imgUrl, stream=True) #获取验证码图片
    im = Image.open(BytesIO(res.content))
    im.show() #显示验证码
    code = input() #人工输入正确的验证码

    #用户名、密码进行转码(根据网站要求)
    loginId = "############"
    password = "############"
    base64_loginId_utf_8 = base64.b64encode(loginId.encode('utf-8'))
    base64_password_utf_8 = base64.b64encode(password.encode('utf-8'))
    ID = re.findall(r'b\'(.*)\'', str(base64_loginId_utf_8)) #进过转码后会出现标志符号b',这里处理掉它
    PWD = re.findall(r'b\'(.*)\'', str(base64_password_utf_8))
    str_loginId_utf_8 = ID[0]
    str_password_utf_8 = PWD[0]

    #构建POST数据并提交
    postData = {'loginId': str_loginId_utf_8,
                'password': str_password_utf_8,
                'vericode': code}
    rs = s.post(loginUrl, postData)

    url = 'http://############/projectInfo.do?method=#######' #登陆成功后的展示地址
    res = s.get(url)

    print(res.text)

if __name__ == '__main__':
    logon()

你可能感兴趣的:(Python)