【发布时间】:2021-01-02 02:50:51
【问题描述】:
我正在尝试使用 R 包 RedditExtractoR 从 Reddit 进行网络抓取。具体来说,我使用 reddit_urls() 从 Reddit 中返回搜索词“总统”的结果。
我首先创建了一个对象links499,它(应该)包含 499 页包含“总统”一词的 URL。我是按 cmets 排序的。
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)
这些对象中的每一个都具有相同数量的唯一 URL 标题 (n=239),并且都只返回了具有非常多 cmets 的 URL(其中最低的是 12,378)。这是有道理的,因为我是按照 cmets 数量递减的顺序从 Reddit 中提取 URL。
# 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)
接下来,我想为 Reddit 中的搜索词“president”返回更多匹配的 URL。我认为这可以通过简单地增加page_threshold 参数来实现。但是,我(未成功)尝试了相同的代码,现在只搜索了 1000 个页面的 URL。
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)
我认为links1000 将包含来自 1000 个 cmet 数量最多的页面中搜索词为“president”的 URL(而links499 将包含来自 499 个页面中搜索词为“president”的 URL厘米)。但是,links1000 和 links499 是相同的。
另外,links1000Com 无法创建并抛出错误: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'。
似乎有 500 页的限制。
我的问题是:接下来我将如何获取所有 URL(及其相关的 cmets)?不仅是前 499 个页面或前 1000 个页面,而是要继续直到在 Reddit 中搜索词为“总统”的所有个 URL 都被返回?
感谢您分享任何建议。
*** 编辑 ***
按照建议,我在下面添加了可重现的代码。再次感谢!
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)
【问题讨论】:
-
当前代码为 sn-p 格式,缺少所需的库调用。一条建议。您可以在 Q 中的某个地方发布您尝试过的完整代码。这样其他人就可以重现问题而不重复代码。我建议在 Q 的末尾添加它。
标签: r web-scraping reddit