steamcyclone commited on
Commit
dac12a4
1 Parent(s): 1a9bb38

Upload redditscraper.py

Browse files
Files changed (1) hide show
  1. redditscraper.py +88 -0
redditscraper.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import time
3
+ import random
4
+
5
+ """This script is used to get many posts from the desired subreddit(s)"""
6
+
7
+ subreddit_list = [
8
+ "theredpillrebooted",
9
+ "RedPillWomen",
10
+ "Feminism",
11
+ "marriedredpill",
12
+ "TheBluePill",
13
+ "PurplePillDebate",
14
+ "RedPillWives",
15
+ ]
16
+ # url = f'https://www.reddit.com/r/{subreddit}/.json?t=all&limit=100'
17
+ url_template = "https://www.reddit.com/r/{}/.json?t=all{}"
18
+
19
+ # headers = {
20
+ # 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
21
+ # }
22
+
23
+ headers = {"User-Agent": "Testing Bot Gundam Wing"}
24
+
25
+ # response = requests.get(url, headers=headers)
26
+
27
+ params = ""
28
+
29
+ counter = 10
30
+ post_list = []
31
+
32
+ for subreddit in subreddit_list:
33
+ while counter > 0:
34
+ print(f"Getting posts with params: {params}")
35
+ print("\n\n\n\n")
36
+ url = url_template.format(subreddit, params)
37
+ response = requests.get(url, headers=headers)
38
+
39
+ if response.ok:
40
+ data = response.json()
41
+ # save data to file
42
+ # with open(f"reddit_{subreddit}_{counter}.json", "w") as f:
43
+ # f.write(response.text)
44
+ posts = data["data"]["children"]
45
+ print(f"Got {len(posts)} posts")
46
+ for post in posts:
47
+ # print(post["data"]["title"])
48
+ pdata = post["data"]
49
+ post_id = pdata["id"]
50
+ title = pdata["title"]
51
+ # url = pdata["url"]
52
+ text = pdata.get("selftext")
53
+ score = pdata["score"]
54
+ author = pdata["author"]
55
+ date = pdata["created_utc"]
56
+ url = pdata.get("url_overridden_by_dest")
57
+ print(f"{post_id}: {title} - {url}")
58
+ # print("Keys are ", pdata.keys())
59
+ # post_list.append(
60
+ # {
61
+ # "id": post_id,
62
+ # "title": title,
63
+ # "text": text,
64
+ # "url": url,
65
+ # "score": score,
66
+ # "author": author,
67
+ # "date": date,
68
+ # "pdata": pdata,
69
+ # }
70
+ # )
71
+ post_list.append(
72
+ [subreddit, post_id, title, text, url, score, author, date, pdata]
73
+ )
74
+ print(f"Got {len(posts)} posts")
75
+ # params = f"?after={data['data']['after']}"
76
+ try:
77
+ params = "&after=" + data["data"]["after"]
78
+ except:
79
+ print(
80
+ "No more posts, broke on ", subreddit, "with counter at ", counter
81
+ )
82
+ break
83
+ counter -= 1
84
+ time.sleep(random.randint(1, 45))
85
+ else:
86
+ print(f"Error: {response.status_code}")
87
+ counter = 10
88
+ params = ""