@agent_2203

Как получить изображение каптчи по запросам и отправить форму на сайт с получением токена?

Столкнулся с такой проблемой как получение каптчи на сайте и последующей отправкой формы, сейчас у меня есть следующий код на Python Selenium:

from extract_functions import *
from requests import get
from image_slicer import slice as slice_img, save_tiles
from os import remove as remove_file
from glob import glob
from random import randint
from time import sleep

class solution_obj(object):
    def __init__(self, driver, url):
        self.driver = driver
        driver.get(url)
        self.switch_iframe(to="captcha")

        self.captcha = self.driver.find_element_by_xpath(
            "//div[@id='rc-anchor-container']")
        self.numb_clicks = 4                                # the maximum number of clicks for the images
    def logout(self):
        self.driver.close()
        self.driver.quit()
    def switch_iframe(self, to):
        self.driver.switch_to.default_content()
        if to == "captcha":
            iframe = self.driver.find_element_by_tag_name("iframe")
            self.driver.switch_to.frame(iframe)
        elif to == "img":
            iframe = self.driver.find_element_by_xpath(
                "//iframe[@title='recaptcha challenge']"
            )
            self.driver.switch_to.frame(iframe)
    def click_on_imgs(self, imgs_on_click):
        imgs_captcha = self.driver.find_elements_by_xpath(
            "//div[@class='rc-image-tile-target']")
        for numb, img in enumerate(imgs_captcha):
            if numb+1 in imgs_on_click:
                img.click()
        self.driver.find_element_by_xpath(
            "//button[@id='recaptcha-verify-button']").click()
    def solving(self):
        # --------------> the stab <--------------
        imgs_on_click_array = []                    # !!!!
        imgs_on_click_len = randint(1, self.numb_clicks)           
        for _ in range(imgs_on_click_len):
            numb_img = randint(1, self.numb_imgs)
            if numb_img not in imgs_on_click_array:
                imgs_on_click_array.append(numb_img)
        print("There are " + str(self.numb_imgs) + " images in total, click on " + str(imgs_on_click_array))
        # --------------> the stab <--------------

        # click on images 
        self.click_on_imgs(imgs_on_click_array)

        # do not forget to delete all imgs after the decision
        files = glob('imgs/*')
        for f in files:
            remove_file(f)
    def get_imgs_of_captha(self):
        self.switch_iframe(to="img")
        self.numb_imgs = len(self.driver.find_elements_by_xpath(         # determining the number of images
            "//td[@class='rc-imageselect-tile']"))          
        
        img = self.driver.find_element_by_xpath(
            "//td[@class='rc-imageselect-tile']")
        link = img.find_element_by_tag_name("img").get_attribute("src")

        req = get(link, allow_redirects=True)
        open('imgs/captcha_img.png', 'wb').write(req.content)

        imgs = slice_img('imgs/captcha_img.png', self.numb_imgs, save=False)
        save_tiles(imgs, directory='imgs', prefix='slice', format='png')     

        return imgs   

def solution(url):
    driver = create_driver()
    obj = solution_obj(driver, url)
    try:
        obj.captcha.click()
        sleep(4)
        imgs = obj.get_imgs_of_captha()

        # while *something*:
        obj.solving() # do something for solution

        # !!! example !!!
        while "Please select all matching images." in obj.driver.page_source:
            sleep(4)
            imgs = obj.get_imgs_of_captha()
            obj.solving()
        # !!! example !!!

    except Exception as e:
        print(e)
    sleep(30)
    obj.logout()


Но все это меня не устраивает, мне нужно сделать прием изображений и отправку формы на сайт исключительно по запросам, есть ли у кого-нибудь варианты, как это сделать ? Мне подойдет любое решение на любом языке, главное реализовать эту идею
  • Вопрос задан
  • 70 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы