Skip to content

Instantly share code, notes, and snippets.

@oursocks
Created August 24, 2019 07:29
Show Gist options
  • Save oursocks/d2a9c588d8ea9c0976f3b3941da7c9d2 to your computer and use it in GitHub Desktop.
Save oursocks/d2a9c588d8ea9c0976f3b3941da7c9d2 to your computer and use it in GitHub Desktop.
How to solve reCAPTCHA v3 using Python for Scraping the Easy Way (Using an OCR API)
import imagetyperzapi2
#... then go to the page using a webdriver object called driver
try:
driver.find_element_by_xpath("//*[@data-sitekey]")
captcha = True
except NoSuchElementException:
captcha = False
if not captcha:
return False
# init imagetyperz api obj
ita = imagetyperzapi2.ImageTyperzAPI(API_KEY)
# get and print account balance
balance = ita.account_balance()
print 'Balance: {}'.format(balance)
if float(balance[1:]) <= 6:
exit("Below funds limit! Exiting.")
p = {}
p['page_url'] = driver.current_url
p['sitekey'] = driver.find_element_by_xpath("//*[@data-sitekey]").get_attribute("data-sitekey")
while True:
try:
captcha_id = ita.submit_recaptcha(p)
break
except Exception as e:
if e == "LIMIT_EXCEED":
sleep(15) # try again later
elif e:
break
while ita.in_progress():
time.sleep(10)
recaptcha_response = ita.retrieve_recaptcha(captcha_id) # captcha_id is optional, if not given, will use last captcha id submited
print 'Recaptcha response: {}'.format(recaptcha_response) # print google response
driver.execute_script("document.getElementById('g-recaptcha-response').innerHTML = '"+recaptcha_response+"';")
# The captcha was successfully solved.
# So, like, login, or whatever.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment