Asyncio如何修复sys:1:RuntimeWarning:coroutine'xxx'从未等待过

2024-05-11 05:07:56 发布

您现在位置:Python中文网/ 问答频道 /正文

构建一个scraper以异步刮取多个页面。我创建了一个类,但一旦我想要运行测试方法,我就会收到2个错误:

TypeError: 'coroutine' object is not callable
sys:1: RuntimeWarning: coroutine 'Scraper.page' was never awaited

这是我的密码:

from requests_html import AsyncHTMLSession

class Scraper(AsyncHTMLSession):
    def __init__(self):
        AsyncHTMLSession.__init__(self)
    
    async def page(self):
        t = await self.get("https://www.somePage.pl/strona/1/")

if __name__ == "__main__":
    t = Scraper()
    d1 = t.run(t.page())
    print(d1.result)

Tags: selfobjectinitisdef错误pagenot