使用上下文管理器管理页面

This commit is contained in:
2023-11-29 11:00:55 +08:00
parent ad635bd37d
commit 75a6989a7f

View File

@@ -38,7 +38,7 @@ def splice_url(url_list: list[str]) -> str:
class Request: class Request:
"""网络请求相关类""" """网络请求相关类"""
_CACHE_FILE = CACHE_PATH.joinpath('cloudflare_cache.json') _CACHE_FILE = CACHE_PATH / 'cloudflare_cache.json'
_headers: dict | None = None _headers: dict | None = None
_cookies: dict | None = None _cookies: dict | None = None
@@ -46,36 +46,31 @@ class Request:
async def _anti_cloudflare(cls, url: str) -> bytes: async def _anti_cloudflare(cls, url: str) -> bytes:
"""用firefox硬穿五秒盾""" """用firefox硬穿五秒盾"""
browser = await BrowserManager.get_browser() browser = await BrowserManager.get_browser()
context = await browser.new_context() async with await browser.new_context() as context, await context.new_page() as page:
page = await context.new_page() response = await page.goto(url)
response = await page.goto(url) attempts = 0
attempts = 0 while attempts < 60: # noqa: PLR2004
while attempts < 60: # noqa: PLR2004 attempts += 1
attempts += 1 text = await page.locator('body').text_content()
text = await page.locator('body').text_content() if text is None:
if text is None: await page.wait_for_timeout(1000)
await page.wait_for_timeout(1000) continue
continue if await page.title() == 'Please Wait... | Cloudflare':
if await page.title() == 'Please Wait... | Cloudflare': logger.warning('疑似触发了 Cloudflare 的验证码')
logger.warning('疑似触发了 Cloudflare 的验证码') break
break
try:
loads(text)
except JSONDecodeError:
await page.wait_for_timeout(1000)
else:
if not isinstance(response, Response):
raise RequestError('api请求失败')
cls._headers = await response.request.all_headers()
try: try:
cls._cookies = {i['name']: i['value'] for i in await context.cookies()} loads(text)
except KeyError: except JSONDecodeError:
cls._cookies = None await page.wait_for_timeout(1000)
await page.close() else:
await context.close() if not isinstance(response, Response):
return await response.body() raise RequestError('api请求失败')
await page.close() cls._headers = await response.request.all_headers()
await context.close() try:
cls._cookies = {i['name']: i['value'] for i in await context.cookies()}
except KeyError:
cls._cookies = None
return await response.body()
raise RequestError('绕过五秒盾失败') raise RequestError('绕过五秒盾失败')
@classmethod @classmethod