1

Web トレンドのトップ URL を表示する Web スクレイパーを構築しています。ただし、常に次のエラーが返されます。

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 に答える