1

正在构建一个网络爬虫,显示网络趋势的热门网址。但是我总是返回以下错误。

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
4

1 回答 1

1

错误在这一行:

hot = data['results'][0]['url']

这意味着以下之一是None

data
data['results']
data['results'][0]

您可以通过连续打印找出哪一个:

print 'data',data
print 'data[results]',data['results']
print 'data[results][0]',data['results'][0]

然后,百万美元的问题将是你json最初是如何解决这个问题的——并弄清楚你需要做什么来处理它(或者如果你能控制这些事情,就阻止它)。:)

于 2013-02-19T04:34:14.880 回答