Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - Unable to validate my query for particular website

I'm trying to query the following website

enter image description here

where Access Number will be a fixed value of 8778791867

And the PIN will be dynamic.

From the normal browser am able to check if it's valid or invalid pin.

But using my code below, I'm unable to get the exact answer as the browser get. as i keep getting invalid for all entries!

import httpx
import trio
import sys
import re
from bs4 import BeautifulSoup
from termcolor import colored
import os

# proxies = {
#     'https': 'http://127.0.0.1:8866'
# }


async def GetValues(client):
    r = await client.get(baseurl)
    soup = BeautifulSoup(r.text, 'lxml')
    return soup.select_one(
        '#__VIEWSTATE')['value'], soup.select_one('#__EVENTVALIDATION')['value']


async def GetToken(client):
    headers = {
        'Content-Type': 'application/x-protobuffer'
    }
    data = "
u0018qc5B-qjP0QEimFYUxcpWJy5Bu0012?u000b03AGdBq264NJCBRJatpe-BlWUNra15cx9i1vtKgAz1cUbsPDMuuhHWpUB1CYdgH3nbhMONc8_uOtU1T3h5hIL0CplyaAt579wUhKd8UshQxTG9L-WRv1W1kNXu8iSF8MzlBxLjrySrHXux6eOm8HG9-oSmdonrvCgv4hkCK43XX_I3leXuhALavOAM06BR-3jYT1Kp0P0OiF8NMyswLsDS9jJUJ-2-290TrTwkuVCEB5YfuJxOvpemh_9iNkmGKRjCbzt5pPoTU_YOp0x2ycmYPNPpPsO9hL35FMiZys6y65q4ynZawQyNYIrAHp4cxAD8Pgj6YmS6Q_cGDTvLsKX60ZeV_W88uWjOC9rTcMHVvpBhbaAukM7p1T2hobQ94fYs1Dabk5LFdRkGUMg-QzWV5OSTXcc-zk7WBQPnF9e4uF2ii3gK1neLoQibb0453SwOp-2PWNbxbjA-nNvj8I-n5ePBV7dkM3P_31E7SOrycN04Wymy5rrV_nX74hLIDBoFph1BOE5aV8f4qu7wHquyPei4Vuq_QoTtLKhmwmSh4xP5-qbrB_N8azx8RTO9EJbMBmgh5FLgqhS4AG-M1YX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Z_I7TlcVufkB2QdCCi', data=data.encode('latin-1'), headers=headers)
    return re.search('"(d.*?)"', r.text).group(1)


async def main(numbers, baseurl):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0"
    }
    # async with httpx.AsyncClient(timeout=None, headers=headers, proxies=proxies, verify=False) as client, trio.open_nursery() as nurse:
    async with httpx.AsyncClient(timeout=None, headers=headers) as client, trio.open_nursery() as nurse:
        async def check(num):
            values = await GetValues(client)
            # token = await GetToken(client)
            data = {
                "ctl00$masterScriptManager": "ctl00$attContent$ctl00|ctl00$attContent$btnSubmit",
                "__EVENTTARGET": "",
                "__EVENTARGUMENT": "",
                "__VIEWSTATE": values[0],
                "__VIEWSTATEGENERATOR": "C5057F25",
                "__EVENTVALIDATION": values[1],
                "ctl00$attContent$txtAccessNumber": "8778791867",
                "ctl00$attContent$txtPIN": num,
                "ctl00$attContent$hdnTier": "",
                # "ctl00$attContent$hdnTokenRecharge": token,
                "ctl00$EmailSignup$txtEmailSignup": "",
                "ctl00$hfUserId": "",
                "__ASYNCPOST": "true",
                "ctl00$attContent$btnSubmit": "Submit"
            }
            r = await client.post(baseurl, data=data)

            if 'Invalid' in r.text:
                print(f"Num: {num}, Status: {colored('Invalid','red')}")
            else:
                print(f"Num: {num}, Status: {colored('Valid','green')}")

        for num in numbers:
            nurse.start_soon(check, num)


if __name__ == "__main__":
    baseurl = 'https://www.virtualprepaidminutes.com/ATT_prepaid_calling_cards_refill_online.aspx'
    numbers = [5418531366, 5418531367]
    trio.run(main, numbers, baseurl)
    
    # if len(sys.argv) == 2:
    #     try:
    #         with open(sys.argv[1]) as f:
    #             numbers = f.read().splitlines()
    #         trio.run(main, numbers, baseurl)
    #     except FileNotFoundError as e:
    #         print(f"File {e.filename} is not exist!")
    # else:
    #     print(f"Usage: python {os.path.basename(__file__)} `InputFile`")

it's should return invalid for 5418531366 and valid for 5418531367

but for some reason i getting invalid for both numbers:

The following response is the actual HTML response.

enter image description here


enter image description here

Thanks in advance.

I would like to handle that out of selenium. as I've built a selenium script for this task too.

question from:https://stackoverflow.com/questions/65644556/unable-to-validate-my-query-for-particular-website

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

1 Answer

0 votes
by (71.8m points)

The site you are trying to automate has integrated google's reCAPTCHA v3 into its form:

 <script src="https://www.google.com/recaptcha/api.js?render=6Le8DfAUAAAAAKsECBdLJ0Z_I7TlcVufkB2QdCCi"></script></style>

This is known issue for web scrapers and automation scripts. The reason you are getting invalid even for a valid submission as the server-side validation for the captcha fails and detects your script as a bot.

reCAPTCHA is described by google as:

reCAPTCHA uses an advanced risk analysis engine and adaptive challenges to keep malicious software from engaging in abusive activities on your website. Meanwhile, legitimate users will be able to login, make purchases, view pages, or create accounts and fake users will be blocked.

Seems you have made an attempt to bypass it in GetToken function but it has obviously failed, I suggest you read more information about reCAPTCHA to understand why. See the official google docs about reCAPTCHA here and this stackoverflow answer.

There is no workaround anyone here could provide you with, there are online paid services that offer captcha solving services. Personally I never had the need to use such service but you can google for them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...