加长网页截图的等待时间,提高截图成功率

This commit is contained in:
CMHopeSunshine 2022-10-08 15:09:06 +08:00
parent 0c2dcacaac
commit 37a1875abc
2 changed files with 6 additions and 6 deletions

View File

@ -29,8 +29,8 @@ post_screenshot = on_regex(r'(https://)?(m\.)?bbs.mihoyo.com/.+/article/\d+', pr
async def _(event: MessageEvent, url: str = RegexMatched()):
logger.info('米游社', f'开始截图帖子<m>{url}</m>')
try:
img = await screenshot(url, elements=['.mhy-article-page__main'])
img = await screenshot(url, elements=['.mhy-article-page__main'], timeout=180000)
except Exception:
logger.info('米游社', f'帖子<m>{url}</m>截图失败')
return
await post_screenshot.finish('米游社帖子截图超时失败了~~')
await post_screenshot.finish(MessageSegment.image(img))

View File

@ -19,7 +19,7 @@ async def start_browser():
global _browser
try:
_playwright = await async_playwright().start()
_browser = await _playwright.chromium.launch()
_browser = await _playwright.chromium.launch(headless=True)
except NotImplementedError:
logger.warning('Playwright', '初始化失败请关闭FASTAPI_RELOAD')
except Exception as e:
@ -49,8 +49,8 @@ async def get_new_page(**kwargs) -> AsyncGenerator[Page, None]:
async def screenshot(url: str,
*,
elements: Optional[Union[List[str]]] = None,
timeout: Optional[float] = 60000,
wait_until: Literal["domcontentloaded", "load", "networkidle"] = "networkidle",
timeout: Optional[float] = 100000,
wait_until: Literal["domcontentloaded", "load", "networkidle", "load", "commit"] = "networkidle",
viewport_size: Tuple[int, int] = (1920, 1080),
full_page=True,
**kwargs):
@ -67,7 +67,7 @@ async def screenshot(url: str,
if not elements:
return await page.screenshot(timeout=timeout, full_page=full_page)
for e in elements:
card = await page.query_selector(e)
card = await page.wait_for_selector(e, timeout=timeout, state='visible')
assert card
clip = await card.bounding_box()
return await page.screenshot(clip=clip, timeout=timeout, full_page=full_page, path='test.png')