-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebscraptitleonly.py
More file actions
40 lines (25 loc) · 1017 Bytes
/
Copy pathwebscraptitleonly.py
File metadata and controls
40 lines (25 loc) · 1017 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
39
40
import csv
import praw
import random
reddit = praw.Reddit(client_id='y1sxdAoN0b-gZQ',
client_secret='56YAQAahxq8XsSR8rakv1nv7y-sOIA',
user_agent='Webscraper')
# get 10 hot posts from the MachineLearning subreddit
topics=['science','food','books']
temporarylist=[]
for topic in topics:
hot_posts = reddit.subreddit(topic).top("year",limit=1000)
for post in hot_posts:
#if len(post.selftext)>0:
# temporarylist.append([post.title.replace('\n','')+'. '+post.selftext.replace('\n','')])
#else:
temporarylist.append([post.title.replace('\n',''),topic])
random.shuffle(temporarylist)
#%%
# field names
fields = ['Content', 'Topic']
# data rows of csv file
with open('3topicstitleonly.csv', 'w', newline='',encoding='utf-8') as f:
write = csv.writer(f,delimiter=',')
write.writerow(fields)
write.writerows(temporarylist)