如何从谷歌使用python获得最后2小时的结果?

时间:2013-04-15 11:56:45

标签: python urllib google-search-api

我有一个搜索主题“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 )

1 个答案:

答案 0 :(得分:2)

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

  

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

     
      
  • 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。