-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrape.py
152 lines (129 loc) · 4.99 KB
/
scrape.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from bs4 import BeautifulSoup
import requests
from newspaper import Article
import boto3
def get_summary(url):
article = Article(url)
article.download()
article.parse()
article.nlp()
return article.summary
def make_news_summary(outputs, site, link, index, headline):
outputs['news'][site][index]['headline'] = headline
outputs['news'][site][index]['link'] = link
outputs['news'][site][index]['summary'] = get_summary(link)
return outputs
def get_ap(outputs):
outputs['news']['AP'] = [{}, {}]
url = 'https://www.apnews.com'
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')
first_story = soup.find('a', class_="headline")
outputs = make_news_summary(
outputs, 'AP', url + first_story['href'],
0, first_story.text)
second_story_container = soup.find('div', class_="RelatedStory")
second_story_link = second_story_container.a
second_story_title = second_story_container.find(attrs={"data-key": "related-story-headline"})
outputs = make_news_summary(
outputs, 'AP', url + second_story_link['href'],
1, second_story_title.text)
return outputs
def get_reuters(outputs):
outputs['news']['Reuters'] = [{}, {}]
url = 'https://www.reuters.com'
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')
first_story_container = soup.find('section', id="topStory")
first_story = first_story_container.find('h2', class_="story-title")
outputs = make_news_summary(
outputs, 'Reuters', url + first_story.a['href'],
0, first_story.text)
second_story_container = soup.find('div', class_="news-headline-list")
second_story_link = second_story_container.find('a')
second_story_title = second_story_container.find('h3', class_="story-title")
outputs = make_news_summary(
outputs, 'Reuters', url + second_story_link['href'],
1, second_story_title.text.strip())
return outputs
def news_run(outputs):
outputs['news'] = {}
outputs = get_ap(outputs)
outputs = get_reuters(outputs)
return outputs
def supreme_court_run(outputs):
url = 'https://www.supremecourt.gov/'
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')
count = 0
for day in soup.find_all(id='opinionsbyday'):
outputs.append({})
date = day.find('span', class_="soday").text
outputs[count][date] = []
names = 0
for ruling in day.find_all('div', class_="casenamerow"):
outputs[count][date].append({})
outputs[count][date][names]['case'] = ruling.span.text
names += 1
summaries = 0
for summary in day.find_all('div', class_="casedetail"):
outputs[count][date][summaries]['summary'] = summary.span.text
summaries += 1
opinions = 0
for opinion in day.find_all('a', target="_blank"):
link = url + opinion['href']
outputs[count][date][opinions]['link'] = link
opinions += 1
count += 1
if count == 3:
break
return outputs
def congress_run(outputs):
url = 'https://www.congress.gov/search?pageSize=25&pageSort=dateOfIntroduction:desc&q={%22source%22:%22legislation%22,%22bill-status%22:%22passed-one%22,%22type%22:%22bills%22}'
source = requests.get(url).text
soup = BeautifulSoup(source, 'lxml')
count = 0
for bill in soup.find_all('li', class_='expanded'):
outputs.append({})
heading = bill.find('span', class_="result-heading")
link = heading.a['href']
outputs[count]['link'] = link
title = bill.find('span', class_="result-title").text
outputs[count]['title'] = title
bill_source = requests.get(link).text
bill_soup = BeautifulSoup(bill_source, 'lxml')
summary_section = bill_soup.find('div', id="bill-summary")
if not summary_section:
outputs[count]['summary'] = 'No summary available'
else:
summary = []
for summary_line in summary_section.find_all('p'):
if not summary_line.a:
summary.append(summary_line.text)
summary = ' '.join(summary)
if summary == '':
outputs[count]['summary'] = 'No summary available'
else:
outputs[count]['summary'] = summary
count += 1
return outputs
def scrape_all():
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('news-site')
new_outputs = {}
new_outputs = news_run(new_outputs)
table.put_item(
Item={'page': 'news',
'content': new_outputs})
sp_outputs = []
sp_outputs = supreme_court_run(sp_outputs)
table.put_item(
Item={'page': 'supreme_court',
'content': sp_outputs})
congress_outputs = []
congress_outputs = congress_run(congress_outputs)
table.put_item(
Item={'page': 'congress',
'content': congress_outputs})
if __name__ == "__main__":
scrape_all()