Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- from pyppeteer import launch
- import requests
- import time
- async def main():
- # launch headless browser
- browser = await launch()
- # open new page
- page = await browser.newPage()
- # URL of the page containing the CAPTCHA challenge
- page_url = 'https://www.google.com/recaptcha/api2/demo'
- # Google sitekey of the CAPTCHA challenge
- sitekey = '6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-'
- # your 2captcha API key
- api_key = '<YOUR_2CAPTCHA_API_KEY>'
- # navigate to the target URL
- await page.goto(page_url)
- print('Making POST request to retrieve CAPTCHA ID')
- # send a request to 2captcha to solve the CAPTCHA challenge
- response = requests.post(f'http://2captcha.com/in.php?key={api_key}&method=userrecaptcha&googlekey={sitekey}&pageurl={page_url}&json=1')
- # retrieve the response
- captcha_id = response.json()['request']
- print('Waiting for 10 seconds after retrieving CAPTCHA ID')
- # wait a few seconds
- time.sleep(10)
- # query 2captcha for the solution with automatic retries
- max_retries = 10 # Maximum number of retries
- solution = ''
- print('Querying for solution...')
- for retry_count in range(max_retries):
- time.sleep(5) # Wait for 5 seconds before querying again
- print(f'Retry {retry_count + 1}...')
- # make GET request
- response = requests.get(f'http://2captcha.com/res.php?key={api_key}&action=get&id={captcha_id}&json=1')
- # retrieve response
- response_data = response.json()
- solution = response_data.get('request', '')
- if solution != 'CAPCHA_NOT_READY': # If solution is obtained, break out of the loop
- break
- if solution == 'CAPCHA_NOT_READY':
- print('Maximum retries reached. CAPTCHA solution not obtained.')
- else:
- print('Solution:', solution)
- # inject the solved CAPTCHA solution into the page and click Submit button
- await page.evaluate('''(solution) => {
- document.getElementById('g-recaptcha-response').innerHTML = solution;
- }''', solution)
- print('Solution injected successfully')
- # Click the submit button
- await page.click('#recaptcha-demo-submit')
- # take a screenshot
- await page.screenshot({'path': 'solved.png'})
- # close the browser
- await browser.close()
- asyncio.get_event_loop().run_until_complete(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement