I created a BaseSpider class that needs to use playwright and httpx, but in the case of coroutines, they conflict. code:
class BaseSpider():
def __init__(self) :
self.play_wright = sync_playwright().start()
async def req(self, client: AsyncClient, i):
res = await client.get('https://www.baidu.com')
print(f'{i + 1},status_code = {res.status_code}')
return res
async def main(self):
async with httpx.AsyncClient() as client:
response = await client.get('https://www.baidu.com')
print(response)
if __name__ == '__main__':
base = BaseSpider()
asyncio.run(base.main())
run error:asyncio.run() cannot be called from a running event loop
I comment # self.play_wright = sync_playwright().start()
.They're ready to work.
How to get Playwright and HTTPX to run together in coroutines?
Thank you for your help!