Как добавить 2captcha-сервис в мой скрипт python? [Дубликат]

2017 ответ: теперь вы можете делать то, что хотите, в каждом текущем браузере и узле

. Это довольно просто:

  • Вернуть обещание
  • Используйте 'await' , в котором JavaScript ожидает, что обещание будет разрешено в vlue (например, hTTP-ответ)
  • Добавьте 'async' для родительской функции

Вот рабочая версия вашего кода:

(async function(){

var response = await superagent.get('...')
console.log(response)

})()

ожидание поддерживается во всех текущих браузерах и узлах 8 [/ д2]

0
задан ZdaR 29 April 2015 в 19:55
поделиться

2 ответа

Посмотрите на модуль запросов

url = 'http://2captcha.com/in.php'
files = {'file': open('image.png', 'rb')}
data = {'key': 'key', 'method': 'post'}
r = requests.post(url, files=files, data=data)
if r.ok:
    # do something with the response data
0
ответ дан Cornel Ghiban 26 August 2018 в 04:35
поделиться

Im из команды 2captcha, и у нас есть образцы python. вы можете использовать код:

"""

Это пример того, как передать google recaptcha v2. Я использовал python + selenium + phantomJS для этого. Phantom JS - это безгласный браузер, используемый в веб-тестировании автоматизации. Я использую его, чтобы ломать страницы и обходить Google captchas. Чтобы использовать это, вы должны установить phantomJS (и построить из источников в системном случае linux) и установленный модуль selenium python

Google captchas появляется на странице в iframe.

"" "

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import *
import re
import os
import sys
import time
import random
import requests
from PIL import Image

class capcha_resolver:
    def __init__(self, captchakey, proxy = None):
        """
       It is assumed that you have phantomjs installed into /bin folder on your linux system.
       """

        self.TWOCAPTCHA_API_KEY = captchakey
        phantom_args = []
        if proxy:
            self.PROXY = proxy
            phantom_args = ['--proxy='+self.PROXY, '--proxy-type=http', '--proxy-type=https']
        self.driver = webdriver.PhantomJS('/bin/phantomjs',service_args=phantom_args)
        self.driver.set_page_load_timeout(20)

    def fail(self, msg):
        print "[!] Error: " + msg
        self.driver.save_screenshot('error.png')

    def get_page(self):
        self.driver.get('https://www.google.com/recaptcha/api2/demo')
        self.driver.save_screenshot('page.png')
        return 0

    def send_capcha(self, filename):
        numbers = []
        captchafile = {'file': open(filename, 'rb')}
        data = {'key': self.TWOCAPTCHA_API_KEY, 'method': 'post'}
        r = requests.post('http://2captcha.com/in.php', files=captchafile, data=data)
        if r.ok and r.text.find('OK') > -1:
            reqid = r.text[r.text.find('|')+1:]
            print "[+] Capcha id: "+reqid
            for timeout in range(40):
                r = requests.get('http://2captcha.com/res.php?key={0}&action=get&id={1}'.format(self.TWOCAPTCHA_API_KEY, reqid))
                if r.text.find('CAPCHA_NOT_READY') > -1:
                    print r.text
                    time.sleep(3)
                if r.text.find('ERROR') > -1:
                    return []
                if r.text.find('OK') > -1:
                    return list(r.text[r.text.find('|')+1:])
        return []

    def bypass_captcha(self):
        """
       Google recaptcha could be found by id. Frame with checkbox has id which starts with I0, recapcha frame has id with I1
       """

        capthcaboxframe = self.driver.find_element_by_xpath('//iframe[starts-with(@id, "I0")]')
        self.driver.switch_to.frame(capthcaboxframe)
        time.sleep(1)
        checkbox = self.driver.find_element_by_id('recaptcha-anchor')
        checkbox.click()
        print "[*] Clicked on checkbox"
        time.sleep(2)
        self.driver.switch_to.default_content()

        capcthaframe = self.driver.find_element_by_xpath('//iframe[starts-with(@id, "I1")]')

        bounding_box = (
            capcthaframe.location['x'], # left
            capcthaframe.location['y'], # upper
            (capcthaframe.location['x'] + capcthaframe.size['width']), # right
            (capcthaframe.location['y'] + capcthaframe.size['height'])) # bottom
        imgname = 'capcha.jpeg' #use jpeg because png images can exceed 2capcha file size limit
        time.sleep(2)
        self.driver.save_screenshot(imgname)
        base_image = Image.open(imgname)
        cropped_image = base_image.crop(bounding_box)
        base_image = base_image.resize(cropped_image.size)
        base_image.paste(cropped_image, (0, 0))
        base_image.save(imgname)

        numbers = self.send_capcha(imgname)
        if numbers == []:
            return -1

        self.driver.switch_to.frame(capcthaframe)
        picturetable = self.driver.find_element_by_css_selector('.rc-imageselect-table-3')
        images = []
        for row in picturetable.find_elements_by_tag_name('tr'):
            for col in row.find_elements_by_tag_name('td'):
                images.append(col.find_element_by_tag_name('img'))
        if images == []:
            self.fail("Found no captcha images")
            return -1

        print "[*] Got answer : " + str(numbers)
        for number in numbers:
            index = int(number) - 1
            images[index].click()
            print '[+] clicked on image '+str(index)
        self.driver.save_screenshot('res.png')
        verifybutton = self.driver.find_element_by_id('recaptcha-verify-button')
        verifybutton.click()
        print "[*] Clicked verify button"
        time.sleep(2)
        if self.driver.find_element_by_css_selector('.rc-imageselect-incorrect-response').is_displayed() or \
                        self.driver.find_element_by_css_selector('.rc-imageselect-error-select-one').is_displayed() or \
                        self.driver.find_element_by_css_selector('.rc-imageselect-error-select-more').is_displayed():
            self.fail("Incorrect answer from 2captcha")
            return -1
        self.driver.switch_to.default_content()

        self.driver.switch_to.frame(capthcaboxframe)
        if self.driver.find_element_by_css_selector('.recaptcha-checkbox').get_attribute('aria-checked') == 'false':
            self.fail("Capctha not passed")
            return -1
        self.driver.switch_to.default_content()
        self.driver.save_screenshot('passed.png')
        return 0

proxy = None
if len(sys.argv) < 2:
    print "Usage: python resolver.py 2CAPCHA_API_KEY [PROXY]"
if len(sys.argv) > 2:
    proxy = sys.argv[2]
resolver = capcha_resolver(sys.argv[1], proxy)

if resolver.get_page() == -1:
    print "[!] Error while getting page"
else:
    print "[+] Opened URL"

if resolver.bypass_captcha() == -1:
    print "[!] Error on captcha resolving"
else:
    print "[+] Resolved captcha"

Удачи вам!

4
ответ дан Zaliko Panjakidze 26 August 2018 в 04:35
поделиться
Другие вопросы по тегам:

Похожие вопросы: