Python 错误:列出索引我们的范围,没有值有效



我正在学习使用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()

您的问题在此行中:

soup.findAll(attrs={"class":"wx-value"}[1].get_text()

具体来说,{"class":"wx-value"}[1].你不能指望列表总是返回一个以上的项目,你永远不知道你会得到多少项目。

相反,您应该查看返回的输出:

soup.findAll(attrs={"class":"wx-value"}.get_text()

并弄清楚如何解析它。

最新更新