Skip to content

Use self.loop.create_task #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 31 additions & 41 deletions src/04-asyncio/web_scraping/async_scrape/program.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,48 @@
import asyncio

import aiohttp
import bs4
from colorama import Fore
import asyncio


async def get_html(episode_number: int) -> str:
print(Fore.YELLOW + f"Getting HTML for episode {episode_number}", flush=True)

url = f'https://talkpython.fm/{episode_number}'

async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
resp.raise_for_status()

return await resp.text()


def get_title(html: str, episode_number: int) -> str:
print(Fore.CYAN + f"Getting TITLE for episode {episode_number}", flush=True)
soup = bs4.BeautifulSoup(html, 'html.parser')
header = soup.select_one('h1')
if not header:
return "MISSING"
class Scraper:

return header.text.strip()
def __init__(self):
self.loop = asyncio.get_event_loop()
self.loop.run_until_complete(self.get_title_range())

async def get_html(self, episode_number: int) -> str:
print(Fore.YELLOW + f"Getting HTML for episode {episode_number}", flush=True)

def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(get_title_range())
print("Done.")
url = f'https://talkpython.fm/{episode_number}'
# resp = requests.get(url)
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
resp.raise_for_status()

return await resp.text()

async def get_title_range_old_version():
# Please keep this range pretty small to not DDoS my site. ;)
for n in range(150, 160):
html = await get_html(n)
title = get_title(html, n)
print(Fore.WHITE + f"Title found: {title}", flush=True)
def get_title(self, html: str, episode_number: int) -> str:
print(Fore.CYAN + f"Getting TITLE for episode {episode_number}", flush=True)
soup = bs4.BeautifulSoup(html, 'html.parser')
header = soup.select_one('h1')
if not header:
return "MISSING"

return header.text.strip()

async def get_title_range():
# Please keep this range pretty small to not DDoS my site. ;)
async def get_title_range(self):
# Please keep this range pretty small to not DDoS my site. ;)

tasks = []
for n in range(150, 160):
tasks.append((n, asyncio.create_task(get_html(n))))
tasks = []
for n in range(150, 160):
tasks.append((n, self.loop.create_task(self.get_html(n))))

for n, t in tasks:
html = await t
title = get_title(html, n)
print(Fore.WHITE + f"Title found: {title}", flush=True)
for n, t in tasks:
html = await t
title = self.get_title(html, n)
print(Fore.WHITE + f"Title found: {title}", flush=True)


if __name__ == '__main__':
main()
Scraper()