首页 > 解决方案 > 有人在 requests_html 中使用 arender() 吗?

问题描述

import asyncio
from requests_html import AsyncHTMLSession

URLS = [
    'https://pythonprogramming.net/parsememcparseface/',
    # ...
    # ...
]

async def send_request(session, url):
    r = await session.get(url)
    # await r.html.arender(timeout=120)
    return r

async def get_tasks(session, urls):
    tasks = []
    for url in urls:
        tasks.append(
            asyncio.create_task(send_request(session, url))
        )
    return tasks

async def main(urls):
    asession = AsyncHTMLSession()
    Tasks = await get_tasks(asession, urls)
    await asession.close()
    return await asyncio.gather(*Tasks)

req = asyncio.run(main(URLS)) # [<Response [200]>]
print(req[0].html.find("#yesnojs", first=True).text)

如果取消注释第 12 行(await r.html.arender(timeout=120))并运行代码结果是成功的,但最后会出现此错误


Look at you shinin!  <--- Result

Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "C:\Users\Shayan\AppData\Local\Programs\Python\Python39\lib\site-packages\pyppeteer\launcher.py", line 152, in _close_process
    self._loop.run_until_complete(self.killChrome())
  File "C:\Users\Shayan\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 617, in run_until_complete
    self._check_closed()
  File "C:\Users\Shayan\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed
sys:1: RuntimeWarning: coroutine 'Launcher.killChrome' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

你能建议另一种方式来呈现页面的 javascript 吗?

标签: pythonpython-asyncio

解决方案


推荐阅读