この BeautifulSoup クローラーのテストで問題が発生しています。Python を始めて 3 時間目なので、明らかな間違いがあればご容赦ください。私はこのコードを以下に持っています...
def huffpost_crawl():
article_list = []
DOMAIN = 'huffingtonpost.com'
huff_soup = BeautifulSoup(urllib2.urlopen("http://www.huffingtonpost.com").read())
news_list = huff_soup.find_all("div", {"class", "snp_most_popular_entry"})[0]
for news in news_list[0]:
title = news('div', {'class', 'snp_most_popular_entry_desc'})[0].a.get_text()
full_url = news('div', {'class', 'snp_most_popular_entry_image'}).a["href"]
blurb = ""
thumb_url = news('div', {'class',
'snp_most_popular_entry_image'}).a.img["longdesc"]
print title
huffpost_crawl()
ターミナルで実行するpythong test.py
と、次のように返されます...
Traceback (most recent call last):
File "test.py", line 21, in <module>
huffpost_crawl()
File "test.py", line 11, in huffpost_crawl
for news in news_list[0]:
File "/usr/local/lib/python2.7/site-packages/bs4/element.py", line 879, in __getitem__
return self.attrs[key]
KeyError: 0