【问题标题】:How get result of last 2 hour from google using python?如何使用 python 从谷歌获取过去 2 小时的结果?
【发布时间】:2013-04-15 11:56:44
【问题描述】:

我有一个搜索主题“leo messi”。我想在过去 2 小时内发布所有关于“leo messi”的博客。现在我被困在这里。这段代码没有过滤博客和时间

import urllib
import json as m_json
query = "leo messi"
query = urllib.urlencode ( { 'q' : query } )
response = urllib.urlopen ( 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&' + query ).read()
json = m_json.loads ( response )
results = json [ 'responseData' ] [ 'results' ]
for result in results:
    title = result['title']
    url = result['url']   # was URL in the original and that threw a name error exception
    print ( title + '; ' + url )

【问题讨论】:

  • 您是否尝试过直接在浏览器上使用生成的 URL 来查看它返回的内容?

标签: python urllib google-search-api


【解决方案1】:

根据this documentation google API 仅提供dateRestrict 参数,允许您添加这些限制:

根据日期将结果限制为 URL。支持的值包括:

  • d[number]:请求过去指定天数的结果。
  • w[number]:请求过去几周指定数量的结果。
  • m[number]:请求过去几个月的指定结果。
  • y[number]:请求过去指定年数的结果。

虽然经过更详细的搜索,我发现this 显示tbs=qdr 参数,可以使用如下:

您可以指定不同的时间段

  • tbs=qdr:s – 前一秒
  • tbs=qdr:n – 前一分钟
  • tbs=qdr:h – 前一小时
  • tbs=qdr:d – 前一天
  • tbs=qdr:w – 前一周
  • tbs=qdr:m – 上个月
  • tbs=qdr:y – 上一年

但我不知道它是否适用于 websearch api。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-29
    • 1970-01-01
    相关资源
    最近更新 更多