Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium.webdriver.common.keys import Keys
- import webbrowser
- import re
- import os
- import json
- import requests
- import random
- import selenium
- from robobrowser import RoboBrowser
- import time
- class vk_captcha:
- """Класс для работы с каптчей. Вырезает img со страницы, отправляет
- в antigate, получает результат, возвращает введенную каптчу в виде строки"""
- def decode(page, root_path):
- """Объединение функций _send_captcha и _check_captcha"""
- print("Каптча...")
- key = "4517fb23f100hvyg87gy87gvhj"
- a_captchaID = vk_captcha._send_captcha(page, root_path, key) #antigate captcha ID [OK, 406704123]
- if a_captchaID[0] != "OK":
- print("Неудалось отправить запрос на antigate\n%s" % a_captchaID)
- return 0
- while True:
- time.sleep(7)
- a_captchaRESULT = vk_captcha._check_captcha(a_captchaID[1], key) #antigate captcah RESULT [OK, Fgv4Kl]
- if a_captchaRESULT[0] != "OK":
- continue
- print("Готово. %s" % a_captchaRESULT[1])
- return a_captchaRESULT[1]
- def _send_captcha(page, root_path, key):
- """Получает страницу, возвращает массив формата [OK, 406704123]"""
- img_tag = page.find(id="captcha")
- if not img_tag:
- img_tag = page.find("img", {"class":"captcha_img"})
- if not img_tag:
- print("Тега с id=captcha не найдено")
- return 0
- img = vk_captcha.request_with_retry(root_path + img_tag["src"]).content
- data = {
- "key": key,
- "method": "post",
- }
- response = vk_captcha.request_with_retry('http://antigate.com/in.php', data=data, files={"file": img})
- return (response.text.split("|"))
- def _check_captcha(captcha_id, key):
- response = vk_captcha.request_with_retry("http://antigate.com/res.php?key=" + key + "&action=get&id=" + captcha_id)
- return (response.text.split('|'))
- def request_with_retry(url, data="", files=""):
- TIMEOUT = 0.5
- while True:
- try:
- if data or files:
- response = requests.post(url, data=data, files=files, timeout=TIMEOUT)
- else:
- response = requests.get(url, timeout=TIMEOUT)
- except requests.exceptions.ReadTimeout:
- continue
- break
- return response
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement