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个回答

附上代码

-*- 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)
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问