Skip to content

Commit 0697e85

Browse files
committed
starter code for python async chapter.
1 parent 8e27ac6 commit 0697e85

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

apps/py/.idea/py.iml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/py/ch10_async/ascrape.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from colorama import Fore
2+
import httpx
3+
import bs4
4+
5+
6+
def main():
7+
print("Using Python packages")
8+
9+
get_titles()
10+
11+
12+
def get_html(n: int) -> str:
13+
print(Fore.YELLOW + f"Getting HTML for episode {n}...", flush=True)
14+
url = f'https://talkpython.fm/{n}'
15+
16+
resp = httpx.get(url)
17+
resp.raise_for_status()
18+
19+
return resp.text
20+
21+
22+
def get_title_from_html(n: int, html: str) -> str:
23+
print(Fore.CYAN + f"Getting TITLE for episode {n}...", flush=True)
24+
25+
soup = bs4.BeautifulSoup(html, 'html.parser')
26+
header = soup.select_one('h1')
27+
if not header:
28+
return "MISSING"
29+
30+
return header.text.strip()
31+
32+
33+
def get_titles():
34+
for n in range(220, 230):
35+
html = get_html(n)
36+
title = get_title_from_html(n, html)
37+
print(Fore.GREEN + title)
38+
39+
40+
if __name__ == '__main__':
41+
main()

0 commit comments

Comments
 (0)