Skip to content

Commit bcab16f

Browse files
committed
starter code for execution pools.
1 parent 09af400 commit bcab16f

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,7 @@ src/07-multiprocessing/.idea/07-multiprocessing.iml
119119
src/07-multiprocessing/.idea/misc.xml
120120
src/07-multiprocessing/.idea/modules.xml
121121
src/07-multiprocessing/.idea/workspace.xml
122+
src/08-execution-pools/.idea/08-execution-pools.iml
123+
src/08-execution-pools/.idea/misc.xml
124+
src/08-execution-pools/.idea/modules.xml
125+
src/08-execution-pools/.idea/workspace.xml

src/08-execution-pools/.idea/vcs.xml

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

src/08-execution-pools/program.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import requests
2+
import bs4
3+
4+
5+
def main():
6+
urls = [
7+
'https://talkpython.fm',
8+
'https://pythonbytes.fm',
9+
'https://google.com',
10+
'https://realpython.com',
11+
'https://training.talkpython.fm/',
12+
]
13+
14+
for url in urls:
15+
print("Getting title from {}".format(url.replace('https', '')),
16+
end='... ',
17+
flush=True)
18+
19+
title = get_title(url)
20+
21+
print("{}".format(title), flush=True)
22+
23+
print("Done", flush=True)
24+
25+
26+
def get_title(url: str) -> str:
27+
resp = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) '
28+
'Gecko/20100101 Firefox/61.0'})
29+
resp.raise_for_status()
30+
31+
html = resp.text
32+
33+
soup = bs4.BeautifulSoup(html, features="html.parser")
34+
tag: bs4.Tag = soup.select_one('h1')
35+
36+
if not tag:
37+
return "NONE"
38+
39+
if not tag.text:
40+
a = tag.select_one('a')
41+
if a and a.text:
42+
return a.text
43+
elif a and 'title' in a.attrs:
44+
return a.attrs['title']
45+
else:
46+
return "NONE"
47+
48+
return tag.get_text(strip=True)
49+
50+
51+
if __name__ == '__main__':
52+
main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
requests
2+
bs4

0 commit comments

Comments
 (0)