Spaces:
Running
Running
Upload hcaptcha_solver.py
Browse files- hcaptcha_solver.py +84 -41
hcaptcha_solver.py
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
import asyncio
|
2 |
import base64
|
3 |
import random
|
4 |
-
import
|
5 |
import nopecha
|
6 |
import requests
|
7 |
from hcaptcha_challenger.agents import Malenia
|
@@ -92,38 +92,56 @@ def url_to_base64(url):
|
|
92 |
return None
|
93 |
|
94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
async def on_response(response, page):
|
96 |
if response.url.startswith("https://api.hcaptcha.com/getcaptcha"):
|
97 |
-
|
98 |
-
content_type = response.headers.get('Content-Type')
|
99 |
-
print(response.headers)
|
100 |
-
print(content_type)
|
101 |
-
if content_type == "application/json":
|
102 |
-
data = await response.json()
|
103 |
-
else:
|
104 |
-
print("content type:", content_type)
|
105 |
-
content = await response.body()
|
106 |
-
print(content)
|
107 |
-
detected_encoding = chardet.detect(content)['encoding']
|
108 |
-
print(detected_encoding)
|
109 |
-
data = content.decode(detected_encoding)
|
110 |
-
# 打印响应内容
|
111 |
-
print(data)
|
112 |
-
examples_urls = data.get("requester_question_example")
|
113 |
-
examples = [url_to_base64(i) for i in examples_urls] if examples_urls else None
|
114 |
-
if data.get("request_type") == "image_label_binary":
|
115 |
-
await classify_click(page, data, 0, examples)
|
116 |
-
elif data.get("request_type") == "image_label_area_select":
|
117 |
-
# await asyncio.sleep(random.uniform(0.1, 0.3))
|
118 |
-
# await page.wait_for_selector("//iframe[contains(@title, 'hCaptcha challenge')]")
|
119 |
-
# frame_challenge = page.frame_locator("//iframe[contains(@title, 'hCaptcha challenge')]")
|
120 |
-
# await frame_challenge.locator("//div[@class='refresh button']").click()
|
121 |
-
await area_click(page, data, 0, examples)
|
122 |
-
else:
|
123 |
-
await asyncio.sleep(random.uniform(0.1, 0.3))
|
124 |
-
await page.wait_for_selector("//iframe[contains(@title, 'hCaptcha challenge')]")
|
125 |
-
frame_challenge = page.frame_locator("//iframe[contains(@title, 'hCaptcha challenge')]")
|
126 |
-
await frame_challenge.locator("//div[@class='refresh button']").click()
|
127 |
elif response.url.startswith("https://api.hcaptcha.com/checkcaptcha"):
|
128 |
data0 = await response.json()
|
129 |
if data0.get("pass"):
|
@@ -134,7 +152,7 @@ async def on_response(response, page):
|
|
134 |
print(data0)
|
135 |
|
136 |
|
137 |
-
async def classify_click(page, data, round0, examples):
|
138 |
try:
|
139 |
await page.wait_for_selector("//iframe[contains(@title, 'hCaptcha challenge')]")
|
140 |
frame_challenge = page.frame_locator("//iframe[contains(@title, 'hCaptcha challenge')]")
|
@@ -142,12 +160,19 @@ async def classify_click(page, data, round0, examples):
|
|
142 |
await frame_challenge.locator("//div[@tabindex='0']").nth(0).wait_for()
|
143 |
count = await samples.count()
|
144 |
print(count)
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
151 |
for i in range(count):
|
152 |
sample = samples.nth(i)
|
153 |
await sample.wait_for()
|
@@ -159,10 +184,26 @@ async def classify_click(page, data, round0, examples):
|
|
159 |
fl = frame_challenge.locator("//div[@class='button-submit button']")
|
160 |
await fl.click()
|
161 |
if round0 == 0:
|
162 |
-
await
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
except Exception as e:
|
164 |
print(e)
|
165 |
-
await classify_click(page, data, round0, examples)
|
166 |
|
167 |
|
168 |
async def area_click(page, data, round0, examples):
|
@@ -216,7 +257,7 @@ async def hit_challenge(context: ASyncContext, host, sitekey, times: int = 8):
|
|
216 |
|
217 |
async def bytedance(host, sitekey):
|
218 |
async with async_playwright() as p:
|
219 |
-
browser = await p.firefox.launch(headless=
|
220 |
context = await browser.new_context(
|
221 |
locale="en-US"
|
222 |
)
|
@@ -243,3 +284,5 @@ async def main(host, key):
|
|
243 |
print(e)
|
244 |
return token
|
245 |
|
|
|
|
|
|
1 |
import asyncio
|
2 |
import base64
|
3 |
import random
|
4 |
+
import re
|
5 |
import nopecha
|
6 |
import requests
|
7 |
from hcaptcha_challenger.agents import Malenia
|
|
|
92 |
return None
|
93 |
|
94 |
|
95 |
+
async def handle_response(page):
|
96 |
+
await asyncio.sleep(2)
|
97 |
+
await page.wait_for_selector("//iframe[contains(@title, 'hCaptcha challenge')]")
|
98 |
+
frame_challenge = page.frame_locator("//iframe[contains(@title, 'hCaptcha challenge')]")
|
99 |
+
question_text = frame_challenge.locator("//div[@class='challenge-prompt']")
|
100 |
+
element_handle = await question_text.element_handle()
|
101 |
+
prompt_padding_element = await element_handle.query_selector("div.prompt-padding")
|
102 |
+
if prompt_padding_element is not None:
|
103 |
+
span_element = await element_handle.query_selector("h2 > span")
|
104 |
+
span_text = await span_element.inner_text()
|
105 |
+
examples0 = frame_challenge.locator('//div[@class="challenge-example"]')
|
106 |
+
examples = examples0.locator('div.image[aria-hidden]')
|
107 |
+
aria_hidden_value = await examples.get_attribute("aria-hidden")
|
108 |
+
if aria_hidden_value == "true":
|
109 |
+
examples = None
|
110 |
+
print("no examples")
|
111 |
+
else:
|
112 |
+
examples1 = examples.locator('//div[@class="image"]')
|
113 |
+
await examples1.wait_for()
|
114 |
+
style_attribute = await examples1.get_attribute("style")
|
115 |
+
url_match = re.search(r'url\("(.+?)"\)', style_attribute)
|
116 |
+
if url_match:
|
117 |
+
url = url_match.group(1)
|
118 |
+
print(url)
|
119 |
+
examples = url
|
120 |
+
task0 = frame_challenge.locator("//div[@class='task-image']")
|
121 |
+
await frame_challenge.locator("//div[@tabindex='0']").nth(0).wait_for()
|
122 |
+
count = await task0.count()
|
123 |
+
print(span_text)
|
124 |
+
print("done tasks for getting examples")
|
125 |
+
data = []
|
126 |
+
for i in range(count):
|
127 |
+
sample = task0.nth(i)
|
128 |
+
img = sample.locator('//div[@class="image"]')
|
129 |
+
style_attribute = await img.get_attribute("style")
|
130 |
+
url_match = re.search(r'url\("(.+?)"\)', style_attribute)
|
131 |
+
if url_match:
|
132 |
+
url = url_match.group(1)
|
133 |
+
data.append(url)
|
134 |
+
print("done tasks for getting url")
|
135 |
+
await classify_click(page, data, 0, examples, span_text)
|
136 |
+
else:
|
137 |
+
await asyncio.sleep(random.uniform(0.1, 0.3))
|
138 |
+
await frame_challenge.locator("//div[@class='refresh button']").click()
|
139 |
+
print("refresh")
|
140 |
+
|
141 |
+
|
142 |
async def on_response(response, page):
|
143 |
if response.url.startswith("https://api.hcaptcha.com/getcaptcha"):
|
144 |
+
asyncio.create_task(handle_response(page))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
elif response.url.startswith("https://api.hcaptcha.com/checkcaptcha"):
|
146 |
data0 = await response.json()
|
147 |
if data0.get("pass"):
|
|
|
152 |
print(data0)
|
153 |
|
154 |
|
155 |
+
async def classify_click(page, data, round0, examples, quetsion0):
|
156 |
try:
|
157 |
await page.wait_for_selector("//iframe[contains(@title, 'hCaptcha challenge')]")
|
158 |
frame_challenge = page.frame_locator("//iframe[contains(@title, 'hCaptcha challenge')]")
|
|
|
160 |
await frame_challenge.locator("//div[@tabindex='0']").nth(0).wait_for()
|
161 |
count = await samples.count()
|
162 |
print(count)
|
163 |
+
if examples:
|
164 |
+
clicks = nopecha.Recognition.solve(
|
165 |
+
type='hcaptcha',
|
166 |
+
task=quetsion0,
|
167 |
+
image_urls=data,
|
168 |
+
image_examples=examples
|
169 |
+
)
|
170 |
+
else:
|
171 |
+
clicks = nopecha.Recognition.solve(
|
172 |
+
type='hcaptcha',
|
173 |
+
task=quetsion0,
|
174 |
+
image_urls=data
|
175 |
+
)
|
176 |
for i in range(count):
|
177 |
sample = samples.nth(i)
|
178 |
await sample.wait_for()
|
|
|
184 |
fl = frame_challenge.locator("//div[@class='button-submit button']")
|
185 |
await fl.click()
|
186 |
if round0 == 0:
|
187 |
+
await asyncio.sleep(2)
|
188 |
+
await page.wait_for_selector("//iframe[contains(@title, 'hCaptcha challenge')]")
|
189 |
+
frame_challenge = page.frame_locator("//iframe[contains(@title, 'hCaptcha challenge')]")
|
190 |
+
task0 = frame_challenge.locator("//div[@class='task-image']")
|
191 |
+
await frame_challenge.locator("//div[@tabindex='0']").nth(0).wait_for()
|
192 |
+
count = await task0.count()
|
193 |
+
data = []
|
194 |
+
for i in range(count):
|
195 |
+
sample = task0.nth(i)
|
196 |
+
img = sample.locator('//div[@class="image"]')
|
197 |
+
style_attribute = await img.get_attribute("style")
|
198 |
+
url_match = re.search(r'url\("(.+?)"\)', style_attribute)
|
199 |
+
if url_match:
|
200 |
+
url = url_match.group(1)
|
201 |
+
data.append(url)
|
202 |
+
print("done tasks for getting url")
|
203 |
+
await classify_click(page, data, 1, examples,quetsion0)
|
204 |
except Exception as e:
|
205 |
print(e)
|
206 |
+
await classify_click(page, data, round0, examples,quetsion0)
|
207 |
|
208 |
|
209 |
async def area_click(page, data, round0, examples):
|
|
|
257 |
|
258 |
async def bytedance(host, sitekey):
|
259 |
async with async_playwright() as p:
|
260 |
+
browser = await p.firefox.launch(headless=False)
|
261 |
context = await browser.new_context(
|
262 |
locale="en-US"
|
263 |
)
|
|
|
284 |
print(e)
|
285 |
return token
|
286 |
|
287 |
+
|
288 |
+
asyncio.run(main("free.vps.vc", "3bae0a5b-f2b8-43ef-98b7-76865a8a3997"))
|