如何在给定时间段内删除所有subreddit帖子

时间:2015-11-24 19:07:42

标签: r web-scraping text-mining reddit

我有一个功能,可以在2014-11-01和2015-10-31之间搜索比特币subreddit中的所有帖子。

但是,我只能提取大约990个帖子,这些帖子可以追溯到10月25日。我不明白发生了什么。在提到https://github.com/reddit/reddit/wiki/API后,我在每个摘录中包含了15秒的Sys.sleep,但没有用。

此外,我尝试从另一个subreddit(健身)进行刮擦,但它也返回了大约900个帖子。

require(jsonlite)
require(dplyr)

getAllPosts <- function() {
    url <- "https://www.reddit.com/r/bitcoin/search.json?q=timestamp%3A1414800000..1446335999&sort=new&restrict_sr=on&rank=title&syntax=cloudsearch&limit=100"
    extract <- fromJSON(url)
    posts <- extract$data$children$data %>% dplyr::select(name, author,   num_comments, created_utc,
                                             title, selftext)  
    after <- posts[nrow(posts),1]
    url.next <- paste0("https://www.reddit.com/r/bitcoin/search.json?q=timestamp%3A1414800000..1446335999&sort=new&restrict_sr=on&rank=title&syntax=cloudsearch&after=",after,"&limit=100")
    extract.next <- fromJSON(url.next)
    posts.next <- extract.next$data$children$data

    # execute while loop as long as there are any rows in the data frame
    while (!is.null(nrow(posts.next))) {
        posts.next <- posts.next %>% dplyr::select(name, author, num_comments, created_utc, 
                                    title, selftext)
        posts <- rbind(posts, posts.next)
        after <- posts[nrow(posts),1]
        url.next <- paste0("https://www.reddit.com/r/bitcoin/search.json?q=timestamp%3A1414800000..1446335999&sort=new&restrict_sr=on&rank=title&syntax=cloudsearch&after=",after,"&limit=100")
        Sys.sleep(15)
        extract <- fromJSON(url.next)
        posts.next <- extract$data$children$data
    }
    posts$created_utc <- as.POSIXct(posts$created_utc, origin="1970-01-01")
    return(posts)
}

posts <- getAllPosts()

reddit是否有某种限制,我正在打击?

1 个答案:

答案 0 :(得分:4)

是的,所有reddit列表(帖子,评论等)上限为1000个项目;出于性能原因,它们基本上只是缓存的列表,而不是查询。

要解决此问题,您需要进行一些巧妙的搜索based on timestamps