Home  >  Q&A  >  body text

Web crawler - What should I do if I always get a verification code error when logging into Bilibili using Python3's requests library?

I suspect that the verification code and the corresponding cookies are out of sync, but I can’t find out what’s wrong.
The code is as follows, I hope someone can help me see it

#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
import chardet
import os
from PIL import Image
from io import BytesIO 


def login():
    #发送登录请求的目标地址
    url = 'https://passport.bilibili.com/login/dologin'
    #发送登录请求所需参数
    act = 'login'
    gourl = 'https://passport.bilibili.com/login/dologin'
    keeptime = '2592000'
    userid = '*****'
    pwd = '*****'
    #获取验证码
    r = requests.get('https://passport.bilibili.com/captcha')
    i = Image.open(BytesIO(r.content))
    i.show()
    vdcode = input('请手动输入验证码:\n')
    #获取与验证码同步的cookies
    cookies = dict(r.cookies)
    #封装所需参数
    data = {'act':act,'gourl':gourl,'keeptime':keeptime,'userid':userid,'pwd':pwd,'vdcode':vdcode}
    #伪装成浏览器
    headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36','Referer':'https://passport.bilibili.com/login'
}
    #发送请求
    r = requests.post(url,data=data,headers=headers,cookies=cookies)
    #通过返回的html代码判断是否登陆成功
    content = r.content.decode()
    print(content)

if __name__ == '__main__':
    login()

阿神阿神2732 days ago796

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-18 11:04:02

    Reference

    reply
    0
  • Cancelreply