-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_estadao_news_today.py
113 lines (101 loc) · 3.62 KB
/
get_estadao_news_today.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
import datetime
import json
import bs4, requests
url = "https://www.estadao.com.br/"
currentDate = datetime.datetime.now()
try:
res = requests.get(url)
soup = bs4.BeautifulSoup(res.text, "lxml")
jsonOutput = []
seenLinks = []
for result in soup.select('.chapeu'):
for linkList in result.select('a'):
output = {
"link": linkList.get('href'),
"year": currentDate.year,
"month": currentDate.month,
"day": currentDate.day
}
if linkList.get('href') in seenLinks:
continue
else:
jsonOutput.append(output)
seenLinks.append(linkList.get('href'))
for result in soup.select('.manchete-content'):
for linkList in result.select('a'):
output = {
"link": linkList.get('href'),
"year": currentDate.year,
"month": currentDate.month,
"day": currentDate.day
}
if linkList.get('href') in seenLinks:
continue
else:
jsonOutput.append(output)
seenLinks.append(linkList.get('href'))
for result in soup.select('.related-news-item'):
for linkList in result.select('a'):
output = {
"link": linkList.get('href'),
"year": currentDate.year,
"month": currentDate.month,
"day": currentDate.day
}
if linkList.get('href') in seenLinks:
continue
else:
jsonOutput.append(output)
seenLinks.append(linkList.get('href'))
for result in soup.select('.bullets'):
for linkList in result.select('a'):
output = {
"link": linkList.get('href'),
"year": currentDate.year,
"month": currentDate.month,
"day": currentDate.day
}
if linkList.get('href') in seenLinks:
continue
else:
jsonOutput.append(output)
seenLinks.append(linkList.get('href'))
for result in soup.select('.carousel-item-especiais'):
for linkList in result.select('a'):
output = {
"link": linkList.get('href'),
"year": currentDate.year,
"month": currentDate.month,
"day": currentDate.day
}
if linkList.get('href') in seenLinks:
continue
else:
jsonOutput.append(output)
seenLinks.append(linkList.get('href'))
for result in soup.select('.carousel-item-blog-coluna'):
for linkList in result.select('a'):
output = {
"link": linkList.get('href'),
"year": currentDate.year,
"month": currentDate.month,
"day": currentDate.day
}
if linkList.get('href') in seenLinks:
continue
else:
jsonOutput.append(output)
seenLinks.append(linkList.get('href'))
data = ""
try:
with open("estadao_today_news.json", "r+") as outfile:
data = json.loads(outfile.read())
for news in jsonOutput:
data['articles'].append(news)
with open("estadao_today_news.json", "w") as outfile:
outfile.write(str(data).replace("'", '"'))
except:
with open("estadao_today_news.json", "w") as outfile:
outfile.write('{"articles": []}')
except Exception as e:
pass