python - 使用python requests模块获取验证码图像的问题

标签 python request captcha

我正在尝试使用请求模块登录具有验证码的网站。

import requests
from bs4 import BeautifulSoup

url="http://somesite.com"
with requests.Session() as s:
  r = s.get(URL, headers=headers)
  soup = BeautifulSoup(r.content, features="html.parser")

  CaptchaImgURL = 'https://somesite.com/image.axd?uid=49a9ca-3a3e-40e7'
  r = requests.get(CapImgURL)
  open('ImageVerify.jpg', 'wb').write(r.content)

  login_details['username']=user
  login_details['passwd']=pwd
  login_details['txtImgVerifyCode']=input('Enter Code:  ')  
  login_details['button']='Log in'
  r = s.post(URL, data=login_details , headers=LoginHeaders)

这里我首先获取登录页面,然后将验证码图像保存在来自 CaptchaImgURL 的单独 URL 请求中。但我在登录时收到无效的验证码。

每次发出请求时,同一个 CaptchaImgURL 中的验证码图像都会发生变化。

第一次请求获取登录页面时如何获取验证码图像?

最佳答案

早些时候,我直接请求验证码图像,而不使用登录的 session requests.Session() 即。

因此,在使用 requests.Session() 发出请求后,即效果很好。

 r = s.get(CapImgURL, headers=headers)

关于python - 使用python requests模块获取验证码图像的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56950182/

相关文章:

javascript - Phaser/PHP动态图片加载

PHP 联系表单返回错误

python - 从多索引中选择时如何避免链式应用?

python - 如何确保Python中的for循环中重复的字符被考虑在内

javascript - promise 不返回请求的值(value)

http - 从响应中读取授权 header

python - 用python连接两个列表列表

python - 使用 aiohttp/asyncio 的异步 HTTP 调用失败并显示 "Cannot connect to host [Network is unreachable]"

python - 在 Python 中使用 URL 查询字符串构建请求

java - 为我的 GWT 应用程序寻找可访问的验证码(支持音频)