forked from jwasham/practice-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.py
More file actions
30 lines (24 loc) · 739 Bytes
/
web.py
File metadata and controls
30 lines (24 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from bs4 import BeautifulSoup
import csv
import json
import requests
def main():
url = 'http://yahoo.com'
req = requests.get(url)
content = req.text
soup = BeautifulSoup(content, "html.parser")
headlines = []
for headline in soup.find_all("h3"):
raw_headline = headline.get_text()
headline = raw_headline.strip()
if len(headline) < 10:
continue
headlines.append(headline)
print(json.dumps(headlines))
with open("headlines-output.csv", 'w') as out_file:
writer = csv.writer(out_file, delimiter=',')
writer.writerow(['headline'])
for headline in headlines:
writer.writerow([headline])
if __name__ == '__main__':
main()