-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathweb_scraping.py
More file actions
30 lines (24 loc) · 747 Bytes
/
web_scraping.py
File metadata and controls
30 lines (24 loc) · 747 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 requests
import csv
import json
def main():
url = 'http://yahoo.com'
req_data = requests.get(url)
content = req_data.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("news_data.csv", 'w') as output_file:
writer = csv.writer(output_file, delimiter=',')
writer.writerow(['headline'])
for headline in headlines:
writer.writerow([headline])
if __name__ == '__main__':
main()