February 2019
Intermediate to advanced
672 pages
16h 50m
English
First, let's look at how to make a request and obtain the HTML source code from a single website with aiohttp. Note that even with only one task (a website), our application remains asynchronous, and the structure of an asynchronous program still needs to be implemented. Now, navigate to the Chapter11/example4.py file, as follows:
# Chapter18/example4.pyimport aiohttpimport asyncioasync def get_html(session, url): async with session.get(url, ssl=False) as res: return await res.text()async def main(): async with aiohttp.ClientSession() as session: html = await get_html(session, 'http://packtpub.com') print(html)loop = asyncio.get_event_loop()loop.run_until_complete(main())
Let's consider the main() coroutine ...