我正在学习使用python作为课程,所以我对这门语言很新。我正在进行一项练习,我一直遇到一个错误,我无法找到解决方案:
File "get-weather-data.py", line 27, in <module>
dayTemp = soup.findAll(attrs={"class":"wx-value"})[1].get_text()
IndexError: list index out of range
我更改了索引编号,似乎没有任何工作 - 我的代码是:
import urllib2
from bs4 import BeautifulSoup
#Create/open file called wunder.txt
f = open('wunder-data.txt', 'w')
#Iternate though months and day
for m in range (1, 13):
for d in range (1, 32):
#Check if already gone through month
if (m == 2 and d > 28):
break
elif (m in [4, 6, 9, 11] and d > 30):
break
# Open wunderground.com url
timestamp = '2009' + str(m) + str(d)
print "Getting data for " + timestamp
url = "http://www.wunderground.com/history/airport/KBUF/2009/"
str(m) + "/" + str(d) + "/dailyhistory.html"
page = urllib2.urlopen(url)
# Get temperature from page
soup = BeautifulSoup(page, "html.parser")
# dayTemp
dayTemp = soup.findAll(attrs={"class":"wx-value"}[1].get_text()
#Format month for timestamp
if len(str(m)) < 2:
mStamp = '0' + str(m)
else:
mStamp = str(m)
#Format day for timestamp
if len(str(d)) < 2:
dStamp = '0' +str(d)
else:
dStamp = str(d)
# Build Timestamp
timestamp = '2009' + mStamp + dStamp
#Write timestamp and temperature to file
f.write(timestamp + ',' +dayTemp + '\n')
# Done
f.Close()
答案 0 :(得分:1)
您的问题在这一行:
soup.findAll(attrs={"class":"wx-value"}[1].get_text()
具体而言,{"class":"wx-value"}[1]
。您不能指望列表总是带有超过1个项目,您永远不会知道您将获得多少项目。
相反,您应该查看从以下位置返回的输出:
soup.findAll(attrs={"class":"wx-value"}.get_text()
并弄清楚如何解析它。