将搜索结果限制为Google自定义搜索API中的特定日期范围

问题描述 投票:1回答:1

在普通Google搜索中,我可以使用工具限制特定日期范围内的搜索结果,并且工作正常.Search in Google site

但是当我在谷歌自定义搜索API中使用相同的限制时,所有结果都将来自而不是特定的日期范围。

假设我希望昨天只发布搜索结果。虽然我在参数中指定了日期范围,但所有结果都会显示出来。但即使早些时候发布的新闻,也不是昨天发布的消息,它应该没有结果。

我见过This answare但它不起作用。 Implementation of google custom search

如果可以将搜索结果限制在特定日期范围内,我想要一个解决方案。我需要在api仪表板中更改任何内容吗?

python google-custom-search
1个回答
0
投票

经过一些测试,这是GOOGLE CSE的\v1\版本希望您在查询中提交的内容。将结果限制为日期范围的结构是:

result = service.cse().list({q:"feedbacklabs",sort:"date:r:20160101:20190101"}).execute()

在python中,这就是我所做的:

# this is in a class function.
from googleapiclient.discovery import build
service = build("customsearch", "v1", developerKey=API_KEY)
# you need an API_KEY for billing. Also note that I've created a 
# custom search engine with the restriction on sites to be none, so 
# essentially searches all of the Internet - but the google docs don't 
# tell you that is an option. I feed it my params like this:

params = dict(
        q=query_string,
        cx=self.search_engine_id, # use google console to build this
        exactTerms=exactTerms, # results must have this word
        linkSite=linkSite, # results must contain a link to this site
        sort=sort) # date range
result = service.cse().list(**params).execute()
# documented here: https://developers.google.com/custom-search/docs/structured_search#restrict-to-range

另请注意,这些日期限制不是很敏感。如果你想要上周或上个月的东西,这很好,但如果你比较一年前到两年前,你基本上得到相同的估计总搜索结果数,四舍五入到最近的百分之一。

© www.soinside.com 2019 - 2024. All rights reserved.