找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1842|回复: 1

[求助] python登录豆瓣失败,但代码我没找到问题

10

主题

15

帖子

15

积分

贫民

积分
15
万里长沙 发表于 2018-7-31 10:53:18 | 显示全部楼层 |阅读模式
程序运行后,打印页面,页面仍然是未登录状态。但没发现哪里有问题啊,求高手指点。(由于**,这里的用户名和密码是随便写的,但测试的时候用的真实有效的账户密码)
  1. # -*- coding: utf-8 -*-
  2. import requests
  3. from bs4 import BeautifulSoup
  4. from PIL import Image
  5. from io import BytesIO

  6. url1 = "https://accounts.douban.com/login"
  7. url2 = "https://www.douban.com/contacts/list"
  8. username = "123456789@@@.com"
  9. passsword = "123456789"

  10. header = {
  11.     "Referer":"https://www.douban.com/login",
  12.     "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
  13.     "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
  14. }

  15. resp**e  = requests.get(url1)
  16. resp**e2 = resp**e.text #获取网页登录页面的HTML代码
  17. # print(resp**e2)

  18. def captcha():  #查找登录页面是否需要验证码
  19.     if "captcha_image" in resp**e2:
  20.         soup1 = BeautifulSoup(resp**e2, 'html.parser')
  21.         soup2 = soup1.find_all(attrs={"name": "captcha-id"})
  22.         soup2 = str(soup2)
  23.         soup3 = BeautifulSoup(soup2, 'html.parser')   #获取验证码ID
  24.         global captchaid
  25.         captchaid = soup3.input["value"]
  26.         # print(captchaid)
  27.         yzm_url1 = soup1.find_all(attrs={"id": "captcha_image"})
  28.         yzm_url1 = str(yzm_url1)
  29.         yzm_url2 = BeautifulSoup(yzm_url1,'html.parser')
  30.         yzm_url3 = yzm_url2.img["src"]        #获取验证码的URL
  31.         yzm_resp**e = requests.get(yzm_url3)
  32.         image = Image.open(BytesIO(yzm_resp**e.content))
  33.         image.show()
  34.         yzm_shuru = input("输入验证码:")
  35.         global captcha_solution
  36.         captcha_solution = yzm_shuru
  37.         global postData_has_captcha
  38.         postData_has_captcha = {         # 有验证码的post数据
  39.             "source": "None",
  40.             "redir": "https://www.douban.com",
  41.             "form_email": username,
  42.             "form_password": passsword,
  43.             "captcha-solution": captcha_solution,
  44.             "captcha-id": captchaid,
  45.             "login": "登录"
  46.         }
  47.         return "有验证码"
  48.     else:
  49.         return "无验证码"

  50. panduan_yzm = captcha()

  51. postData_no_captcha ={     #无验证码的post数据
  52.     "source":"None",
  53.     "redir":"https://www.douban.com",
  54.     "form_email":username,
  55.     "form_password":passsword,
  56.     "login":"登录"
  57. }

  58. def denglu_has_captcha():   #有验证码的登录函数
  59.     global s
  60.     s = requests.session()
  61.     f = s.post(url1,header,postData_has_captcha)

  62. def denglu_no_captcha():   #无验证码的登录函数
  63.     global s
  64.     s = requests.session()
  65.     f = s.post(url1,header,postData_no_captcha)

  66. if panduan_yzm == "有验证码":   #开始登陆
  67.     denglu_has_captcha()
  68.     print("有验证码,已登录")
  69.     r = s.get(url2)
  70.     print(r.text)
  71. elif panduan_yzm == "无验证码":
  72.     denglu_no_captcha()
  73.     print("无验证码,已登录")
  74.     r = s.get(url2)
  75.     print(r.text)
  76. else:
  77.     pass
复制代码




回复

使用道具 举报

10

主题

15

帖子

15

积分

贫民

积分
15
万里长沙  楼主| 发表于 2018-7-31 10:57:36 | 显示全部楼层
论坛自动把“resp**e”替换为“resp**e”了,应该不影响理解。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表