سؤال

So i curentlly run this script that i found from http://www.dcortesi.com/blog/2008/05/28/google-ajax-search-api-example-python-code/

import urllib
import simplejson
query = urllib.urlencode({'q' : 'the.hobbit.2012.imdb'})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' \
 % (query)
search_results = urllib.urlopen(url)
json = simplejson.loads(search_results.read())
results = json['responseData']['results']
for i in results:
 if "imdb" in i['url']:
  print i['url']
  break

What i want is simply to get the first result from google containing imdb. (i need the movie id number)

My problem is, after like 4-6 searches i keep getting for about 15 seconds, then i can do 1 search again.

Traceback (most recent call last):
  File "./g", line 9, in <module>
    results = json['responseData']['results']
TypeError: 'NoneType' object is unsubscriptable

From what i have read google only allows a certin ammount of searches a day etc. But they should allow more then 10 searches a minute?

What else could be the problem here? Or are there any other better way to search google? I only need the "highest" result that links to imdb.

هل كانت مفيدة؟

المحلول

Google's Web Search API is deprecated (and rate limit enforcement is being tightened), so you have a couple options (in order of my preference):

  1. Scrap the Google search altogether and use imdbpy.
  2. Use Google Custom Search API to avoid rate limit.

IMDbPY Example

>>> import imdb    
>>> ia = imdb.IMDb()    
>>> movies = ia.search_movie(title='The Hobbit: An Unexpected Journey')    
>>> movies[0].movieID    
'0903624'
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top