Why does Python3 simulate login to a website CAPTCHA always wrong?

problem description

Python3 simulates logging in to a website verification code is always wrong

the environmental background of the problems and what methods you have tried

first requested the interface to get the CAPTCHA, and then POST submitted the login form, but the CAPTCHA has been wrong, Baidu failed, if there is a big god is willing to give advice one or two thank you very much!

related codes

/ / Please paste the code text below (do not replace the code with pictures)

-sharp -*- coding: utf-8 -*-
from urllib import parse

import requests

-sharp python2  python3
try:
    -sharp python2 
    import cookielib
    print(f"user cookielib in python2.")
except:
    -sharp python3 
    import http.cookiejar as cookielib
    print(f"user cookielib in python3.")


-sharp session
mSession = requests.session()
-sharp session.cookies save()cookielibLWPCookieJarcookiesave
mSession.cookies = cookielib.LWPCookieJar(filename = "mafengwoCookies.txt")


login_url="https://dianpos-agent.91dbq.com/agent/operatorLogin.action"


-sharp
url = "https://dianpos-agent.91dbq.com/agent/main.action"

randomCode="-0"

userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
header = {
    "origin": "https://dianpos-agent.91dbq.com",
    "Referer": "https://dianpos-agent.91dbq.com/agent/operatorLogout.action",
    "User-Agent": userAgent,
}

def login(account, password):
    -sharp 
    print ("")

    print(mSession.cookies)
    codeurl = "https://dianpos-agent.91dbq.com/agent/generateRandomCode.action"
    valcode = mSession.get(codeurl)
    print(mSession.cookies)
    f = open("valcode.png", "wb")
    -sharp response
    f.write(valcode.content)
    -sharp 
    f.close()
    code = input(":")
    randomCode=str(code)
    postData = {
        "username": account,
        "password": password,
        "randomCode": randomCode
    }
    print(mSession.cookies)
    print(postData)
    postData=parse.urlencode(postData).encode("utf-8")
    -sharp sessionpost
    responseRes = mSession.post(login_url, data=postData, headers=header)
    print(mSession.cookies)
    -sharp  statusCode = 200
    print(f"statusCode = {responseRes.status_code}")
    print(f"text = {responseRes.text}")
    -sharp cookie
    mSession.cookies.save()


if __name__ == "__main__":
    login("xxx", "xxx")

what result do you expect? What is the error message actually seen?

I hope to get the CAPTCHA correctly, submit it and log in successfully!

Apr.24,2022

import requests
from pyquery import PyQuery as Q

url = 'https://dianpos-agent.91dbq.com/agent/main.action'
login_url='https://dianpos-agent.91dbq.com/agent/operatorLogin.action'
code_url = 'https://dianpos-agent.91dbq.com/agent/generateRandomCode.action'

session = requests.session()
session.headers = {
    'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
}

def login(account, password):
    valcode = session.get(code_url)
    with open('valcode.png', 'wb') as f:
        f.write(valcode.content)

    code = raw_input('input code:')

    data = {
        "username": account,
        "password": password,
        "randomCode": code
    }

    r = session.post(login_url, data=data)
    print Q(r.text).find('.errorMessage').text()


if __name__ == '__main__':
    login("xxx", "xxx")
Menu