Python lib beautiful soup using aiohttp

Viewed 9247

someone know how to do :

import html5lib
import urllib
from bs4 import BeautifulSoup

soup = BeautifulSoup(urllib.request.urlopen('http://someWebSite.com').read().decode('utf-8'), 'html5lib')

using aiohttp instead of urllib ?

Thanks ^^

2 Answers

Just for people who is looking for more answers: there is another way to run synchronous code in loop: loop.run_in_executor.

More in docs: https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.loop.run_in_executor

Example code:

import asyncio
import time

def blocking_func():
    time.sleep(5)
    return 42

async def main(loop):
    result = await loop.run_in_executor(None, blocking_func)
    return result

loop = asyncio.get_event_loop()
loop_result = loop.run_until_complete(main(loop))
print(loop_result) # => 42

So, you can await your task like you do it with coroutine

Related