Advertisement
dereksir

Untitled

Feb 13th, 2024 (edited)
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. import asyncio
  2. from pyppeteer import launch
  3. import requests
  4. import time
  5.  
  6. async def main():
  7.     # launch headless browser
  8.     browser = await launch()
  9.     # open new page
  10.     page = await browser.newPage()
  11.  
  12.     # URL of the page containing the CAPTCHA challenge
  13.     page_url = 'https://www.google.com/recaptcha/api2/demo'
  14.    
  15.     # Google sitekey of the CAPTCHA challenge
  16.     sitekey = '6Le-wvkSAAAAAPBMRTvw0Q4Muexq9bi0DJwx_mJ-'
  17.  
  18.     # your 2captcha API key
  19.     api_key = '<YOUR_2CAPTCHA_API_KEY>'
  20.  
  21.     # navigate to the target URL
  22.     await page.goto(page_url)
  23.  
  24.  
  25.     print('Making POST request to retrieve CAPTCHA ID')
  26.     # send a request to 2captcha to solve the CAPTCHA challenge
  27.     response = requests.post(f'http://2captcha.com/in.php?key={api_key}&method=userrecaptcha&googlekey={sitekey}&pageurl={page_url}&json=1')
  28.     # retrieve the response
  29.     captcha_id = response.json()['request']
  30.  
  31.     print('Waiting for 10 seconds after retrieving CAPTCHA ID')
  32.     # wait a few seconds
  33.     time.sleep(10)
  34.  
  35.     # query 2captcha for the solution with automatic retries
  36.     max_retries = 10  # Maximum number of retries
  37.     solution = ''
  38.     print('Querying for solution...')
  39.     for retry_count in range(max_retries):
  40.         time.sleep(5)  # Wait for 5 seconds before querying again
  41.         print(f'Retry {retry_count + 1}...')
  42.         # make GET request
  43.         response = requests.get(f'http://2captcha.com/res.php?key={api_key}&action=get&id={captcha_id}&json=1')
  44.         # retrieve response
  45.         response_data = response.json()
  46.         solution = response_data.get('request', '')
  47.         if solution != 'CAPCHA_NOT_READY':  # If solution is obtained, break out of the loop
  48.             break
  49.  
  50.     if solution == 'CAPCHA_NOT_READY':
  51.         print('Maximum retries reached. CAPTCHA solution not obtained.')
  52.     else:
  53.         print('Solution:', solution)
  54.  
  55.     # inject the solved CAPTCHA solution into the page and click Submit button
  56.     await page.evaluate('''(solution) => {
  57.        document.getElementById('g-recaptcha-response').innerHTML = solution;        
  58.    }''', solution)
  59.     print('Solution injected successfully')
  60.  
  61.     # Click the submit button
  62.     await page.click('#recaptcha-demo-submit')
  63.  
  64.     # take a screenshot
  65.     await page.screenshot({'path': 'solved.png'})
  66.  
  67.     # close the browser
  68.     await browser.close()
  69.  
  70. asyncio.get_event_loop().run_until_complete(main())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement