-
Notifications
You must be signed in to change notification settings - Fork 0
/
spider.py
269 lines (208 loc) · 7.97 KB
/
spider.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import requests
from bs4 import BeautifulSoup
import urllib
import locale
from datetime import datetime, timedelta
import sys
class Spider:
def __init__(self):
self._query = ''
self._comeback_days = 2
locale.setlocale(locale.LC_ALL, 'pt_BR.utf8')
@property
def query(self):
return self._query
@query.setter
def query(self, new_query):
''' search query '''
self._query = urllib.parse.quote_plus(new_query)
def _getSoup(self, url):
headers = {
'User-Agent': 'Mozilla/5.0 \
(Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7)' +
'Gecko/2009021910 Firefox/3.0.7',
}
try:
with requests.Session() as s:
download = s.get(url, headers=headers)
decoded_content = download.content.decode('UTF-8')
soup = BeautifulSoup(decoded_content, 'html.parser')
except Exception as e:
print(f'Erro ao raspar dados: {e}')
return False
return soup
def getEstadao(self):
url = "https://busca.estadao.com.br/?tipo_conteudo=Todos&quando=nas-ultimas-24-horas&q=" + \
str(self._query)
soup = self._getSoup(url)
content = soup.find_all(
'section', {'class': 'col-md-6 col-sm-6 col-xs-12 col-margin'})
news = {}
for index, row in enumerate(content):
_datetime = self.string_cleaner(
row.span.find_next('span').get_text())
news[index] = {
'href': row.a['href'],
'title': row.a['title'],
'headline': row.p.get_text(),
'author': row.span.get_text(),
'datetime': _datetime,
}
return news
def getValor(self):
url = "https://valor.globo.com/busca/?q=" + \
str(self._query) + "&page=1&order=recent&from=now-1d"
soup = self._getSoup(url)
content = soup.find_all(
'div', {'class': 'widget--info__title product-color'})
news = {}
for index, row in enumerate(content):
news[index] = {
'href': row.parent['href'],
'title': self.string_cleaner(row.get_text()),
'headline': row.parent.p.get_text(),
'datetime': row.parent.css.select(
".widget--info__meta")[0].get_text(),
}
return news
def getFolhaSP(self):
url = "https://search.folha.uol.com.br/search?q=" + \
str(self._query) + "&periodo=24&sd=&ed=&site=todos"
soup = self._getSoup(url)
content = soup.find_all(
'div', {'class': 'c-headline__content'})
news = {}
for index, row in enumerate(content):
try:
news[index] = {
'href': row.a['data-href'],
'title': self.string_cleaner(row.h2.get_text()),
'headline': self.string_cleaner(row.p.get_text()),
'datetime': self.string_cleaner(row.time.get_text()),
}
# else:
# break
return news
except Exception as err:
print(f"Erro ao raspar Folha de SP: {err}")
return False
def getOGlobo(self):
url = "https://oglobo.globo.com/busca/?q=" + \
str(self._query) + "&order=recent&from=now-1d"
soup = self._getSoup(url)
content = soup.find_all(
'div', {'class': 'widget--info__title product-color'})
news = {}
for index, row in enumerate(content):
news[index] = {
'href': row.parent['href'],
'title': self.string_cleaner(row.get_text()),
'headline': self.string_cleaner(row.parent.p.get_text()),
'datetime': row.parent.css.select(
".widget--info__meta")[0].get_text(),
}
# else:
# break
# print(news)
return news
def getValorInveste(self):
url = "https://valorinveste.globo.com/busca/?q=" + \
str(self._query) + "&order=recent&from=now-1d"
soup = self._getSoup(url)
content = soup.find_all(
'div', {'class': 'widget--info__title product-color'})
news = {}
for index, row in enumerate(content):
news[index] = {
'href': row.parent['href'],
'title': self.string_cleaner(row.get_text()),
'headline': self.string_cleaner(row.parent.p.get_text()),
'datetime': row.parent.css.select(
".widget--info__meta")[0].get_text(),
}
return news
def getEInvestidor(self):
url = "https://einvestidor.estadao.com.br/?s=" + \
str(self._query)
soup = self._getSoup(url)
content = soup.find_all(
'h2')
news = {}
for index, row in enumerate(content):
news[index] = {
'href': row.parent['href'],
'title': self.string_cleaner(row.get_text()),
'headline': self.string_cleaner(row.parent.p.get_text()),
'datetime': row.parent.css.select(
".widget--info__meta")[0].get_text(),
}
return news
def getPipeline(self):
url = "https://pipelinevalor.globo.com/busca/?q=" + \
str(self._query) + "&order=recent&from=now-1d"
soup = self._getSoup(url)
content = soup.find_all(
'div', {'class': 'widget--info__title product-color'})
news = {}
for index, row in enumerate(content):
news[index] = {
'href': row.parent['href'],
'title': self.string_cleaner(row.get_text()),
'headline': self.string_cleaner(row.parent.p.get_text()),
'datetime': row.parent.css.select(
".widget--info__meta")[0].get_text(),
}
return news
def getNeofeed(self):
url = "https://neofeed.com.br/?s=" + \
str(self._query)
soup = self._getSoup(url)
content = soup.find_all('h3')
news = {}
for index, row in enumerate(content):
# date
_datetime = self.string_cleaner(row.parent.parent.parent.css.select(
"span.date")[0].get_text())
date = _datetime.split('/')
day = date[0]
mounth_str = str(date[1])
year = date[2]
datetime_str = f"{day}-{mounth_str}-{year}"
datetime_format = '%d-%m-%y'
datetime_obj = datetime.strptime(datetime_str, datetime_format)
# timedelta
previous_days = timedelta(days=self._comeback_days)
now = datetime.now()
isOld = datetime_obj < (now - previous_days)
if (isOld):
break
news[index] = {
'href': row.parent['href'],
'title': self.string_cleaner(row.get_text()),
'headline': self.string_cleaner(row.parent.parent.parent.p.get_text()),
'datetime': _datetime,
}
return news
def getBrazilJournal(self):
url = "https://braziljournal.com/?s=" + \
str(self._query)
soup = self._getSoup(url)
content = soup.find_all('h2', {'class': 'boxarticle-infos-title'})
news = {}
for index, row in enumerate(content):
if (index == 3):
break
news[index] = {
'href': row.a['href'],
'title': self.string_cleaner(row.a.get_text()),
'headline': '',
'datetime': '',
}
return news
def string_cleaner(self, text):
text = str(text)
text = text.replace('\n', ' ')
text = text.replace('\r', ' ')
text = text.rstrip(' ')
text = text.lstrip(' ')
return text