mirror of
https://github.com/xuthus83/LittlePaimon.git
synced 2024-10-21 16:27:15 +08:00
✨ 加长网页截图
的等待时间,提高截图成功率
This commit is contained in:
parent
0c2dcacaac
commit
37a1875abc
@ -29,8 +29,8 @@ post_screenshot = on_regex(r'(https://)?(m\.)?bbs.mihoyo.com/.+/article/\d+', pr
|
|||||||
async def _(event: MessageEvent, url: str = RegexMatched()):
|
async def _(event: MessageEvent, url: str = RegexMatched()):
|
||||||
logger.info('米游社', f'开始截图帖子<m>{url}</m>')
|
logger.info('米游社', f'开始截图帖子<m>{url}</m>')
|
||||||
try:
|
try:
|
||||||
img = await screenshot(url, elements=['.mhy-article-page__main'])
|
img = await screenshot(url, elements=['.mhy-article-page__main'], timeout=180000)
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.info('米游社', f'帖子<m>{url}</m>截图失败')
|
logger.info('米游社', f'帖子<m>{url}</m>截图失败')
|
||||||
return
|
await post_screenshot.finish('米游社帖子截图超时失败了~~')
|
||||||
await post_screenshot.finish(MessageSegment.image(img))
|
await post_screenshot.finish(MessageSegment.image(img))
|
||||||
|
@ -19,7 +19,7 @@ async def start_browser():
|
|||||||
global _browser
|
global _browser
|
||||||
try:
|
try:
|
||||||
_playwright = await async_playwright().start()
|
_playwright = await async_playwright().start()
|
||||||
_browser = await _playwright.chromium.launch()
|
_browser = await _playwright.chromium.launch(headless=True)
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
logger.warning('Playwright', '初始化失败,请关闭FASTAPI_RELOAD')
|
logger.warning('Playwright', '初始化失败,请关闭FASTAPI_RELOAD')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -49,8 +49,8 @@ async def get_new_page(**kwargs) -> AsyncGenerator[Page, None]:
|
|||||||
async def screenshot(url: str,
|
async def screenshot(url: str,
|
||||||
*,
|
*,
|
||||||
elements: Optional[Union[List[str]]] = None,
|
elements: Optional[Union[List[str]]] = None,
|
||||||
timeout: Optional[float] = 60000,
|
timeout: Optional[float] = 100000,
|
||||||
wait_until: Literal["domcontentloaded", "load", "networkidle"] = "networkidle",
|
wait_until: Literal["domcontentloaded", "load", "networkidle", "load", "commit"] = "networkidle",
|
||||||
viewport_size: Tuple[int, int] = (1920, 1080),
|
viewport_size: Tuple[int, int] = (1920, 1080),
|
||||||
full_page=True,
|
full_page=True,
|
||||||
**kwargs):
|
**kwargs):
|
||||||
@ -67,7 +67,7 @@ async def screenshot(url: str,
|
|||||||
if not elements:
|
if not elements:
|
||||||
return await page.screenshot(timeout=timeout, full_page=full_page)
|
return await page.screenshot(timeout=timeout, full_page=full_page)
|
||||||
for e in elements:
|
for e in elements:
|
||||||
card = await page.query_selector(e)
|
card = await page.wait_for_selector(e, timeout=timeout, state='visible')
|
||||||
assert card
|
assert card
|
||||||
clip = await card.bounding_box()
|
clip = await card.bounding_box()
|
||||||
return await page.screenshot(clip=clip, timeout=timeout, full_page=full_page, path='test.png')
|
return await page.screenshot(clip=clip, timeout=timeout, full_page=full_page, path='test.png')
|
||||||
|
Loading…
Reference in New Issue
Block a user