继上一稿破解cf-turnstile-response,本人研究了第二种解决方法,那就是不依赖打开开发者,直接通过自己来定义一个HTML,然后通过sync_playwright().start().chromium.launch()来拦截请求把自己定义的HTML加载进入做到模拟生成正确的cf-turnstile-response,接下来让我们一直来操作实像生成
一、HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>body's solver</title>
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?onload=onloadTurnstileCallback" async="" defer=""></script>
</head>
<body>
<!--<div class="cf-turnstile" data-sitekey="******************"></div>-->
<span>cf_turnstile:</span>
<!-- cf turnstile -->
</body>
</html>
先简单制作一个HTML(暂时先不去美观先,有空在修复)
二、拦截请求加载html
创建solver.py
import time
import random
class Solver:
def __init__(self, playwright, proxy="", headless=True):
self.playwright = playwright
self.proxy = proxy
self.headless = headless
self.start_browser(self.playwright)
def solve(self, url, sitekey, invisible=False):
self.url = url
self.sitekey = sitekey
self.invisible = invisible
self.context = self.browser.contexts[0]
# self.page = self.browser.new_page()
self.page = self.context.pages[0]
self.build_page_data()
self.page.route(self.url, lambda route: route.fulfill(body=self.page_data, status=200))
self.page.goto(self.url)
self.current_x = 0
self.current_y = 0
self.window_width = self.page.evaluate("window.innerWidth")
self.window_height = self.page.evaluate("window.innerHeight")
output = "failed"
return output
def start_browser(self, playwright):
self.browser = playwright.chromium.connect_over_cdp('http://localhost:9999')
以上是整个拦截和加载的
拦截成功,本地的html好自动加载cf-turnstile-response。
这里留了一个坑,希望你可以自己发现。实在不太理解可以私,这种比去浏览器加载剩好多时间,基本都是依赖于本地的html,通过中途去拦截实现
有兴趣可以QQ群讨论:679666897