Reddit 搜索 API 未给出所有结果
Reddit search API not giving all results
import praw
def get_data_reddit(search):
username=""
password=""
r = praw.Reddit(user_agent='')
r.login(username,password,disable_warning=True)
posts=r.search(search, subreddit=None,sort=None, syntax=None,period=None,limit=None)
title=[]
for post in posts:
title.append(post.title)
print len(title)
search="Whosebug"
get_data_reddit(search)
Ouput=953
为什么要限制?
- [文档][1] 提及
We can at most get 1000 results from every listing, this is an
upstream limitation by reddit. There is nothing we can do to go past
this limit. But we may be able to get the results we want with the
search() method instead.
有什么解决方法吗?我希望以某种方式克服 API,我为 twitter 数据写了一个 scraper,发现它不是最有效的解决方案。
同样的问题:https://github.com/praw-dev/praw/issues/430
[1]: https://praw.readthedocs.org/en/v2.0.15/pages/faq.html
相关讨论也请参考上述link
所以我会简单地遍历一组预先确定的搜索查询,我假设时间段是一个时间段?我也不确定它的格式是什么,所以下面大部分都是编造的,但你应该明白要点。
在这种情况下它会像下面这样
import praw
def get_data_reddit(search):
username=""
password=""
r = praw.Reddit(user_agent='')
r.login(username,password,disable_warning=True)
title=[]
periods = (time1, time2, time3, time4) # declare a set of times to use in the search query to limit results
for period in periods: # loop through the different time points and query the posts from that time.
posts=r.search(search, subreddit=None,sort=None, syntax=None,period=None,limit=None) # this now returns a limited search query.
for post in posts:
title.append(post.title) # and append as usual.
print len(title)
search="Whosebug"
get_data_reddit(search)
限制搜索或列表的结果是减少服务器负载的常用策略。 reddit API 很清楚这就是它的作用(正如您已经标记的那样)。然而,它并不止于此...
API 还支持列表分页结果的变体。由于它是一个不断变化的数据库,它们不提供页面,而是允许您使用 'after' 参数从中断的地方继续。这已记录在案 here。
现在,虽然我不熟悉 PRAW,但我看到 reddit search API 符合列表语法。因此,我认为您只需要重新发出搜索,指定额外的 'after' 参数(指的是第一次搜索的最后结果)。
经过随后的试用,PRAW 似乎真正返回了您所要求的所有结果。
应 OP 的要求,这是我编写的用于查看分页结果的代码。
import praw
def get_data_reddit(search, after=None):
r = praw.Reddit(user_agent='Whosebug example')
params = {"q": search}
if after:
params["after"] = "t3_" + str(after.id)
posts = r.get_content(r.config['search'] % 'all', params=params, limit=100)
return posts
search = "Whosebug"
post = None
count = 0
while True:
posts = get_data_reddit(search, post)
for post in posts:
print(str(post.id))
count += 1
print(count)
import praw
def get_data_reddit(search):
username=""
password=""
r = praw.Reddit(user_agent='')
r.login(username,password,disable_warning=True)
posts=r.search(search, subreddit=None,sort=None, syntax=None,period=None,limit=None)
title=[]
for post in posts:
title.append(post.title)
print len(title)
search="Whosebug"
get_data_reddit(search)
Ouput=953
为什么要限制?
- [文档][1] 提及
We can at most get 1000 results from every listing, this is an upstream limitation by reddit. There is nothing we can do to go past this limit. But we may be able to get the results we want with the search() method instead.
有什么解决方法吗?我希望以某种方式克服 API,我为 twitter 数据写了一个 scraper,发现它不是最有效的解决方案。
同样的问题:https://github.com/praw-dev/praw/issues/430 [1]: https://praw.readthedocs.org/en/v2.0.15/pages/faq.html 相关讨论也请参考上述link
所以我会简单地遍历一组预先确定的搜索查询,我假设时间段是一个时间段?我也不确定它的格式是什么,所以下面大部分都是编造的,但你应该明白要点。
在这种情况下它会像下面这样
import praw
def get_data_reddit(search):
username=""
password=""
r = praw.Reddit(user_agent='')
r.login(username,password,disable_warning=True)
title=[]
periods = (time1, time2, time3, time4) # declare a set of times to use in the search query to limit results
for period in periods: # loop through the different time points and query the posts from that time.
posts=r.search(search, subreddit=None,sort=None, syntax=None,period=None,limit=None) # this now returns a limited search query.
for post in posts:
title.append(post.title) # and append as usual.
print len(title)
search="Whosebug"
get_data_reddit(search)
限制搜索或列表的结果是减少服务器负载的常用策略。 reddit API 很清楚这就是它的作用(正如您已经标记的那样)。然而,它并不止于此...
API 还支持列表分页结果的变体。由于它是一个不断变化的数据库,它们不提供页面,而是允许您使用 'after' 参数从中断的地方继续。这已记录在案 here。
现在,虽然我不熟悉 PRAW,但我看到 reddit search API 符合列表语法。因此,我认为您只需要重新发出搜索,指定额外的 'after' 参数(指的是第一次搜索的最后结果)。
经过随后的试用,PRAW 似乎真正返回了您所要求的所有结果。
应 OP 的要求,这是我编写的用于查看分页结果的代码。
import praw
def get_data_reddit(search, after=None):
r = praw.Reddit(user_agent='Whosebug example')
params = {"q": search}
if after:
params["after"] = "t3_" + str(after.id)
posts = r.get_content(r.config['search'] % 'all', params=params, limit=100)
return posts
search = "Whosebug"
post = None
count = 0
while True:
posts = get_data_reddit(search, post)
for post in posts:
print(str(post.id))
count += 1
print(count)