forked from railbotan/xmljson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetNewsJSON.py
More file actions
38 lines (29 loc) · 991 Bytes
/
getNewsJSON.py
File metadata and controls
38 lines (29 loc) · 991 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
31
32
33
34
35
36
37
38
import xml.etree.ElementTree as ET
from urllib.request import urlopen
import json
data = urlopen('https://lenta.ru/rss').read().decode('utf8')
root = ET.fromstring(data)
channel = root[0]
def getAllNews():
allNews = []
for i in channel.findall('item'):
allNews.append({'pubDate': i.find('pubDate').text, 'title': i.find('title').text})
return allNews
def getFinalFile():
final_file = json.dumps(getAllNews(), ensure_ascii=False).encode('utf8')
with open('news.json', 'wb') as file:
file.write(final_file)
getFinalFile()
def getNewsForTaskTwo():
allNewsTwo = []
for item in channel.findall('item'):
fields = {}
for field in item:
fields[field.tag] = field.text
allNewsTwo.append(fields)
return allNewsTwo
def getFinalFile2():
final_file = json.dumps(getNewsForTaskTwo(), ensure_ascii=False).encode('utf8')
with open('news2.json', 'wb') as file:
file.write(final_file)
getFinalFile2()