jonny789
jonny789

Reputation: 343

Problem in getting captcha image using python requests module

I am trying to login to a site having captcha using requests module.

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)

Here I am fetching login page first and then saving captcha image in a separate url request from CaptchaImgURL. But I get invalid captcha code while log in.

The captcha image in the same CaptchaImgURL changes everytime when I make a request.

How can I fetch the captcha image when I make request to fetch login page for the first time ?

Upvotes: 2

Views: 3055

Answers (1)

jonny789
jonny789

Reputation: 343

Earlier I was making direct request for captcha image without using the logged in session requests.Session() i.e. s.

So after making request using requests.Session() i.e. s. It worked fine.

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

Upvotes: 1

Related Questions