我们通过谷歌找到了这段代码。它应该为我们提供关键字的 Google 索引。问题是它工作了一段时间然后给我们这个错误:
./g1.py size hassize
Traceback (most recent call last):
File "./g1.py", line 22, in <module>
n2 = int(gsearch(args[0]+" "+args[1])['cursor']['estimatedResultCount'])
TypeError: 'NoneType' object is unsubscriptable
编码:
#!/usr/bin/env python
import math,sys
import json
import urllib
def gsearch(searchfor):
query = urllib.urlencode({'q': searchfor})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
search_response = urllib.urlopen(url)
search_results = search_response.read()
results = json.loads(search_results)
data = results['responseData']
return data
args = sys.argv[1:]
m = 45000000000
if len(args) != 2:
print "need two words as arguments"
exit
n0 = int(gsearch(args[0])['cursor']['estimatedResultCount'])
n1 = int(gsearch(args[1])['cursor']['estimatedResultCount'])
n2 = int(gsearch(args[0]+" "+args[1])['cursor']['estimatedResultCount'])