Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python lib beautiful soup using aiohttp

Tags:

python

aiohttp

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 ^^

like image 296
APianist Avatar asked May 25 '17 13:05

APianist


Video Answer


1 Answers

You can do something like this:

import asyncio
import aiohttp
import html5lib
from bs4 import BeautifulSoup

SELECTED_URL = 'http://someWebSite.com'

async def get_site_content():
    async with aiohttp.ClientSession() as session:
        async with session.get(SELECTED_URL) as resp:
            text = await resp.read()

    return BeautifulSoup(text.decode('utf-8'), 'html5lib')

loop = asyncio.get_event_loop()
sites_soup = loop.run_until_complete(get_site_content())
print(sites_soup)
loop.close()
like image 77
Yuval Pruss Avatar answered Oct 13 '22 09:10

Yuval Pruss