-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
43 lines (34 loc) · 1.04 KB
/
main.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
import requests
import json
import pytz
from datetime import datetime
from pprint import pprint as pp
from bs4 import BeautifulSoup
base_url = 'https://www.cnn.com/europe/live-news/russia-ukraine-war-news-{}-{}-{}{}/index.html'
tz_WA = pytz.timezone('US/Pacific')
datetime_WA = datetime.now(tz_WA)
url1 = base_url.format(
str(datetime_WA.month).rjust(2, '0'),
str(datetime_WA.day).rjust(2, '0'),
str(datetime_WA.year)[-2:],
'-intl'
)
url2 = base_url.format(
str(datetime_WA.month).rjust(2, '0'),
str(datetime_WA.day).rjust(2, '0'),
str(datetime_WA.year)[-2:],
''
)
print(url1 + '\n' + url2)
text = requests.get(url1).text
soup = BeautifulSoup(text, features='lxml')
headers = soup.select("h2.sc-dfVpRl.kvaBeP")
if headers == []:
text = requests.get(url2).text
soup = BeautifulSoup(text, features='lxml')
headers = soup.select("h2.sc-dfVpRl.kvaBeP")
#'<h2 class="sc-dfVpRl kvaBeP">'
headers = list(map(lambda x: x.text, headers[1:]))
#print(headers)
with open('data.data', 'w') as f:
f.write('\n'.join(headers))