I am attempting to webscrape from Reddit using the R package RedditExtractoR. Specifically, I am using reddit_urls() to return results from Reddit with the search term "president".
I first created an object links499 that (should) contain 499 pages worth of URLs that contain the term "president". I sorted by comments.
links499 <- reddit_urls(search_terms = "president",
cn_threshold = 0,
page_threshold = 499,
sort_by = "comments",
wait_time = 2)
links499Com <- get_reddit(search_terms = "president",
cn_threshold = 0,
page_threshold = 499,
sort_by = "comments",
wait_time =2)
Each of these objects had the same number of unique URL titles (n=239) and both only returned URLs with very high number of comments (the lowest of which was 12,378). This makes sense because I am pulling URLs from Reddit in order of decreasing number of comments.
# Have the same number of unique titles
unique(links499$title)
unique(links499Com$title)
# Both have minimum of 12378
min(links499$num_comments)
min(links499Com$num_comments)
I next wanted to return an even larger amount of matched URLs for the search term "president" from Reddit. I thought this could be accomplished by simply increasing the page_threshold parameter. However, I (unsuccessfully) tried the same code only now searching through 1,000 pages worth of URLs.
links1000 <- reddit_urls(search_terms = "president",
cn_threshold = 0,
page_threshold = 1000,
sort_by = "comments",
wait_time = 2)
links1000Com <- get_reddit(search_terms = "president",
cn_threshold = 0,
page_threshold = 1000,
sort_by = "comments",
wait_time =2)
I thought links1000 would contain URLs with search term "president" from the 1000 pages with the largest number of comments (whereas links499 would contain URLs with search term "president" from the 499 pages with the largest number of comments). However, links1000 and links499 were identical.
Moreover, links1000Com could not be created and threw an error: URL 'https://www.reddit.com/r/politics/comments/dzd8lu/discussion_thread_fifth_democratic_presidential/.json?limit=500': status was 'Failure when receiving data from the peer'.
It seems there is a 500 page limit.
My question is: How would I next obtain all URLs (and their associated comments)? Not just for the top 499 pages or top 1000 pages but to continue until all URLs with the search term "president" in Reddit have been returned?
Thank you for sharing any advice.
*** EDIT ***
As suggested, I am adding reproducible code below. Thank you again!
library(tidyverse)
library(RedditExtractoR)
links499 <- reddit_urls(search_terms = "president",
cn_threshold = 0, # minimum number of comments
page_threshold = 499,
sort_by = "comments",
wait_time = 2)
links499Com <- get_reddit(search_terms = "president",
cn_threshold = 0,
page_threshold = 499,
sort_by = "comments",
wait_time =2)
# Have the same number of unique titles (n=239)
length(unique(links499$title))
length(unique(links499Com$title))
# Both have minimum of 12378
min(links499Com$num_comments)
min(links499$num_comments)
links1000 <- reddit_urls(
search_terms = "president",
cn_threshold = 0, # minimum number of comments
page_threshold = 1000, # can probably get as many URLs as you want but you can only extract a certain amount of data at one time
sort_by = "comments",
wait_time = 2
)
links1000Com <- get_reddit(search_terms = "president",
cn_threshold = 0,
page_threshold = 1000,
sort_by = "comments",
wait_time =2 )
# Have the same number of unique titles (n=241)
length(unique(links1000$title))
length(unique(links1000Com$title))
# Both have minimum of 12378
min(links1000Com$num_comments)
min(links1000$num_comments)