-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreddit.py
53 lines (30 loc) · 976 Bytes
/
reddit.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
import requests
import json
def format_date(timestamp_obj):
from datetime import datetime
datetime_obj= datetime.fromtimestamp(timestamp_obj)
return str(datetime_obj)
def get_data(url):
response=requests.get(url,headers={'User-agent':'your bot 0.1'})
python_object=json.loads(response.text)
news=python_object['data']['children']
filter_data=[]
number=1
for new in news:
news_data={
f'news number {number}':{
'title': new['data']['title'],
'author':new['data']['author'],
'created':format_date(new['data']['created'])
}
}
filter_data.append(news_data)
number+=1
return filter_data
def write_to_json(data):
with open('RedditNews.json','w') as json_file:
json.dump(data,json_file,indent=2)
def main(url):
data=get_data(url)
write_to_json(data)
main('https://www.reddit.com/r/science/.json')