正在构建一个网络爬虫,显示网络趋势的热门网址。但是我总是返回以下错误。
Traceback (most recent call last):
File "D:\Ceryx\webSearch.py", line 21, in <module>
topl=webScraper(m)
File "D:\Ceryx\webSearch.py", line 12, in webScraper
hot = data['results'][0]['url']
TypeError: 'NoneType' object has no attribute '__getitem__'
帮助!!
import re
import json
import urllib, urllib2
def webScraper(trends):
query=urllib.urlencode({'q':trends})
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s' % query
response = urllib.urlopen(url)
extract = response.read()
results = json.loads(extract)
data = results['responseData']
hot = data['results'][0]['url']
return hot
response = urllib2.urlopen('http://www.google.com/trends/hottrends/atom/hourly')
html = response.read()
matchObj = re.findall(r'<a[^>]*?>(.*?)</a>', html)
print "Urls"
for m in matchObj:
topl=webScraper(m)
print m,topl