Solving h-captcha on scrapy

FreeMag

New member
#1
I was scraping using Selenium and when ever there was a captcha I was using 2captcha service to solve the captcha with something like the following:
Code:
from twocaptcha import TwoCaptcha
solver = TwoCaptcha(CAPTCHA_KEY)
captcha = response.xpath("//div[@class='h-captcha' and @data-sitekey]").get()
if captcha:
    url = driver.current_url
    site_key = WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='h-captcha']"))).get_attribute("data-sitekey")
    result = solver.hcaptcha(sitekey=site_key, url=url)
    driver.execute_script('document.getElementsByName("h-captcha-response")[0].innerHTML = "{}"'.format(result['code']))
    time.sleep(1)
    driver.find_element_by_xpath("/html/body/form/input").click()

I moved on now using scrapy but I stragle to find a way to use 2captcha service with scrapy when captcha is happening. I do get the result but not sure how to use to move one:

solver = TwoCaptcha(CAPTCHA_KEY)
captcha = response.xpath("//div[@class='h-captcha' and @data-sitekey]").get()
if captcha:
    site_key = response.xpath("//div[@class='h-captcha']/@data-sitekey").get()
    result = self.solver.hcaptcha(sitekey=site_key, url=response.url)
    # AFTER WHAT??

I tried something like:

`scrapy.FormRequest.from_response(response, formdata={'h-captcha-response': result['code']}, callback=self.parse)`
Where self.parse the main function to scrape, but it doesnt seem to work.