Python 3.x python请求使用验证码登录

Python 3.x python请求使用验证码登录,python-3.x,web-scraping,python-requests,Python 3.x,Web Scraping,Python Requests,我正在尝试下载一个验证码图像,手动解决它,然后在帖子中提交它以及用户名和密码。我的响应文本只是最初的登录页面,所以我认为这意味着我的代码失败了。我要登录的网页是在黑暗的网络上,但我不知道这是否真的相关。我唯一能想到的是,提交帖子的行为就是生成一个新的验证码。希望对HTTP有更好理解的人能帮助我 from bs4 import BeautifulSoup import base64 import requests session = requests.Session() session.prox

我正在尝试下载一个验证码图像,手动解决它,然后在帖子中提交它以及用户名和密码。我的响应文本只是最初的登录页面,所以我认为这意味着我的代码失败了。我要登录的网页是在黑暗的网络上,但我不知道这是否真的相关。我唯一能想到的是,提交帖子的行为就是生成一个新的验证码。希望对HTTP有更好理解的人能帮助我

from bs4 import BeautifulSoup
import base64
import requests

session = requests.Session()
session.proxies = {'http': 'socks5h://127.0.0.1:9150', 'https': 'socks5h://127.0.0.1:9150'}

url = session.get("http://waeixxcraed4gw7q.onion/signin")
soup = BeautifulSoup(url.text, "lxml")
imgs = soup.findAll('img')

#save captcha from base64 encoding
img_data = bytes(imgs[1]['src'][23:],encoding='utf-8')
with open("olympus_captcha.jpg","wb") as fh:
    fh.write(base64.decodestring(img_data))

#solve the captcha that has been saved to the harddrive    
captcha = input("enter captcha:\n")

#attempt login (password and username removed)
payload = {"username":username, "password":password, "captcha":captcha}
response = session.post("http://waeixxcraed4gw7q.onion/signin", data = payload)
print(response.text)

我是这样做的, 首先在您的系统上安装

 session = requests.session()
 url = "Login page link"
 r = session.get(url)
 soup = bs(r.text, 'html.parser')
 img = soup.find('img', id='captcha')
 img_SRC = img['src']

 with open('captcha.jpg', 'wb') as handle:
     response = requests.get(img_SRC, stream=True)
     if not response.ok:
         print("ok")
     for block in response.iter_content(1024):
         if not block:
             break
         handle.write(block)
  try:
      from PIL import Image
  except ImportError:
      import Image
  import pytesseract
  pic = Image.open('result/captcha.jpg')
  pytesseract.pytesseract.tesseract_cmd = r'Enter the installation path of the \tesseract.exe'
  captchaText = pytesseract.image_to_string(pic)
Capcha中的文本可用于登录