open source guy
open source guy

Reputation: 2787

How get result of last 2 hour from google using python?

I have a search topic "leo messi". I want to get all blog about "leo messi" posted in last 2 hours. Now i am stuck here.This code not filtering blog and time

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 )

Upvotes: 1

Views: 961

Answers (1)

Vyktor
Vyktor

Reputation: 20997

According to this documentation google API provides only dateRestrict parameter which allows you to add those restrictions:

Restricts results to URLs based on date. Supported values include:

  • d[number]: requests results from the specified number of past days.
  • w[number]: requests results from the specified number of past weeks.
  • m[number]: requests results from the specified number of past months.
  • y[number]: requests results from the specified number of past years.

Although after more detailed search I found this which shows tbs=qdr parameter which could be used as followed:

You can specify different time periods

  • tbs=qdr:s – previous second
  • tbs=qdr:n – previous minute
  • tbs=qdr:h – previous hour
  • tbs=qdr:d – previous day
  • tbs=qdr:w – previous week
  • tbs=qdr:m – previous month
  • tbs=qdr:y – previous year

But I have no idea whether it'll work with websearch api.

Upvotes: 3

Related Questions