Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
223 views
in Technique[技术] by (71.8m points)

authentication - How to download captcha and then login in python

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...