搜索

首页  >  问答  >  正文

网页爬虫 - 用Python3的requests库模拟登陆Bilibili总是提示验证码错误怎么办?

我怀疑是验证码和对应的cookies不同步,但我也检查不出来哪里错了
代码如下,望哪位大神帮我看看

#!/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()

阿神阿神2750 天前812

全部回复(1)我来回复

  • 大家讲道理

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

    参考

    回复
    0
  • 取消回复