xiangm2 2017-03-10 09:22 采纳率: 0%
浏览 2269

python requests模拟登录B站验证码问题

求问如何保证我获取的验证码和和执行模拟登录的验证码是一致的啊
附上代码

-*- coding : cp936 -*-

import time
import datetime
import requests
import traceback
from PIL import Image

First get the picList

session = requests.Session()
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0',
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Connection': 'keep-alive',
'Host':'passport.bilibili.com','Referer': 'https://passport.bilibili.com/login',
'Accept':'*/*','Accept-Encoding':'gzip, deflate, br'}

def getPicCode():
picurl = 'http://passport.bilibili.com/captcha'
try:
payload = {'t':''}
pictext = session.get(url = picurl,headers = headers)
print pictext.status_code
#print pictext.request.headers
with open('codepic.jpg','wb') as result:
result.write(pictext.content)
except Exception, diag:
print str(diag)

def LoginBili(userid,password,vdcode):
getPicCode()
image = Image.open("codepic.jpg")
image.show()
vdcode = raw_input('Please input the code in the Pic:')
loginurl = 'https://passport.bilibili.com/login/dologin'
data = {'act':'login','gourl':'http://www.bilibili.com/','keeptime':'2592000',
'pwd':password,'userid':userid,'vdcode':vdcode}
print data
r = session.post(url = loginurl,headers = headers,data = data)
print r.status_code
print r.text

if name == '__main__':
password = '**************'
userid,vdcode = '**********','0'
LoginBili(userid, password, vdcode)

每次需要手动输入自己获取的验证码,但是不知道为什么,每次显示输入的验证码都是错误的,应该是两次访问页面,这个验证码变化了,不知道要怎么调整

  • 写回答

1条回答 默认 最新

  • xiangm2 2017-03-10 09:28
    关注

    附上代码

    -*- coding : cp936 -*-

    import time
    import datetime
    import requests
    import traceback
    from PIL import Image

    First get the picList

    session = requests.Session()
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0',
    'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Connection': 'keep-alive',
    'Host':'passport.bilibili.com','Referer': 'https://passport.bilibili.com/login',
    'Accept':'*/*','Accept-Encoding':'gzip, deflate, br'}

    def getPicCode():
    picurl = 'http://passport.bilibili.com/captcha'
    try:
    payload = {'t':''}
    pictext = session.get(url = picurl,headers = headers)
    print pictext.status_code
    #print pictext.request.headers
    with open('codepic.jpg','wb') as result:
    result.write(pictext.content)
    except Exception, diag:
    print str(diag)

    def LoginBili(userid,password,vdcode):
    getPicCode()
    image = Image.open("codepic.jpg")
    image.show()
    vdcode = raw_input('Please input the code in the Pic:')
    loginurl = 'https://passport.bilibili.com/login/dologin'
    data = {'act':'login','gourl':'http://www.bilibili.com/','keeptime':'2592000',
    'pwd':password,'userid':userid,'vdcode':vdcode}
    print data
    r = session.post(url = loginurl,headers = headers,data = data)
    print r.status_code
    print r.text

        if __name__ == '__main__':
    password ='***************'
    userid,vdcode = '**********','0'
    LoginBili(userid, password, vdcode)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog