I'm having issues trying to login into a website. I download captcha img and then manually input the verification text, but apparently when i download the captcha a new one is generated, making invalid the one i downloaded... any suggestions to fix this?
import requests
from bs4 import BeautifulSoup
url = "someurl"
user_agent = useragent'
usr="MyUser"
pw= "password"
with requests.Session() as s:
r = s.get(url, headers={'user-agent': user_agent, }, cookies=s.cookies)
# print(s.cookies.get_dict())
# print(r.status_code)
soup = BeautifulSoup(r.content, features="html.parser")
viewstate = soup.find(id="__VIEWSTATE")['value']
viewstate_gen = soup.find(id="__VIEWSTATEGENERATOR")['value']
event_val = soup.find(id="__EVENTVALIDATION")['value']
# print(viewstate)
# print(viewstate_gen)
# print(event_val)
captcha_html = soup.find(id="ctl00_cphPage_Login_ImageCaptcha")
# print(captcha_html)
captcha_url = url + captcha_html['src']
r = s.get(captcha_url, headers={'user-agent': user_agent, }, cookies=s.cookies)
# print(s.cookies.get_dict())
open('ImageVerify.jpg', 'wb').write(r.content)
print("image_saved")
captcha_txt=input("captcha: ")
loguin_data = {
'__EVENTTARGET': '',
'__EVENTARGUMENT': '',
'__LASTFOCUS': '',
'__VIEWSTATE': viewstate,
'__VIEWSTATEGENERATOR': viewstate_gen,
'__EVENTVALIDATION': event_val,
'Language': 'es-MX',
'Currency': '',
'ctl00$txtSearch': '',
'ctl00$cphPage$Login$UserName': usr,
'ctl00$cphPage$Login$Password': pw,
'ctl00$cphPage$Login$capcha': captcha_txt,
'ctl00$cphPage$Login$LoginButton': 'Entrar',
'ctl00$taxes$listCountries': '54',
}
r=s.post(url, data=loguin_data, headers={'user-agent': user_agent, }, cookies=s.cookies)
soup = BeautifulSoup(r.content, features="html.parser")
print(r.status_code)
# print("The cookie is:")
# print(s.cookies.get_dict())
question from:
https://stackoverflow.com/questions/66051818/how-to-download-captcha-and-then-login-in-python 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…