|
程序运行后,打印页面,页面仍然是未登录状态。但没发现哪里有问题啊,求高手指点。(由于**,这里的用户名和密码是随便写的,但测试的时候用的真实有效的账户密码)
- # -*- coding: utf-8 -*-
- import requests
- from bs4 import BeautifulSoup
- from PIL import Image
- from io import BytesIO
- url1 = "https://accounts.douban.com/login"
- url2 = "https://www.douban.com/contacts/list"
- username = "123456789@@@.com"
- passsword = "123456789"
- header = {
- "Referer":"https://www.douban.com/login",
- "User-Agent":"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
- "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
- }
- resp**e = requests.get(url1)
- resp**e2 = resp**e.text #获取网页登录页面的HTML代码
- # print(resp**e2)
- def captcha(): #查找登录页面是否需要验证码
- if "captcha_image" in resp**e2:
- soup1 = BeautifulSoup(resp**e2, 'html.parser')
- soup2 = soup1.find_all(attrs={"name": "captcha-id"})
- soup2 = str(soup2)
- soup3 = BeautifulSoup(soup2, 'html.parser') #获取验证码ID
- global captchaid
- captchaid = soup3.input["value"]
- # print(captchaid)
- yzm_url1 = soup1.find_all(attrs={"id": "captcha_image"})
- yzm_url1 = str(yzm_url1)
- yzm_url2 = BeautifulSoup(yzm_url1,'html.parser')
- yzm_url3 = yzm_url2.img["src"] #获取验证码的URL
- yzm_resp**e = requests.get(yzm_url3)
- image = Image.open(BytesIO(yzm_resp**e.content))
- image.show()
- yzm_shuru = input("输入验证码:")
- global captcha_solution
- captcha_solution = yzm_shuru
- global postData_has_captcha
- postData_has_captcha = { # 有验证码的post数据
- "source": "None",
- "redir": "https://www.douban.com",
- "form_email": username,
- "form_password": passsword,
- "captcha-solution": captcha_solution,
- "captcha-id": captchaid,
- "login": "登录"
- }
- return "有验证码"
- else:
- return "无验证码"
- panduan_yzm = captcha()
- postData_no_captcha ={ #无验证码的post数据
- "source":"None",
- "redir":"https://www.douban.com",
- "form_email":username,
- "form_password":passsword,
- "login":"登录"
- }
- def denglu_has_captcha(): #有验证码的登录函数
- global s
- s = requests.session()
- f = s.post(url1,header,postData_has_captcha)
- def denglu_no_captcha(): #无验证码的登录函数
- global s
- s = requests.session()
- f = s.post(url1,header,postData_no_captcha)
- if panduan_yzm == "有验证码": #开始登陆
- denglu_has_captcha()
- print("有验证码,已登录")
- r = s.get(url2)
- print(r.text)
- elif panduan_yzm == "无验证码":
- denglu_no_captcha()
- print("无验证码,已登录")
- r = s.get(url2)
- print(r.text)
- else:
- pass
复制代码
|
|