[英]Restrict search results to a specific date range in google custom search api
在普通的 Google 搜索中,我可以使用工具将搜索结果限制在特定日期范围内:
但是当我对 Google 自定义搜索 API 使用相同的限制时,所有结果都会出现,而不是特定日期范围。
假设我只想在昨天发布搜索结果。 尽管我在参数中指定了日期范围,但所有结果都将显示出来。 但是,即使新闻发布较早,而不是昨天,它也应该不会返回任何结果。
我看过这个答案:
在 Google 自定义搜索 api 中指定日期范围
但它不起作用。
如果可以或不可以将搜索结果限制在特定日期范围内,我想要一个解决方案。 我需要更改 API 仪表板中的任何内容吗?
只需使用 Google 的操作符before:和after: 。
经过一些测试,这就是 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
另请注意,这些日期限制不是很敏感。 如果您想要上周或上个月的内容,那很好,但是如果您将一年前与两年前进行比较,您基本上会得到相同的总搜索结果估计数量,四舍五入到最接近的百位。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.