beautifulsoup "list对象没有属性"错误

5 投票
2 回答
16545 浏览
提问于 2025-04-17 18:35

我正在尝试从一个天气网站上抓取温度数据,使用的代码如下:

    import urllib2
from BeautifulSoup import BeautifulSoup

f = open('airport_temp.tsv', 'w')

f.write("Location" + "\t" + "High Temp (F)" + "\t" + "Low Temp (F)" + "\t" + "Mean Humidity" + "\n" )

eventually parse from http://www.wunderground.com/history/airport/\w{4}/2012/\d{2}/1/DailyHistory.html

for x in range(10):
    locationstamp = "Location " + str(x)
    print "Getting data for " + locationstamp
    url = 'http://www.wunderground.com/history/airport/KAPA/2013/3/1/DailyHistory.html'

    page = urllib2.urlopen(url)
    soup = BeautifulSoup(page)

    location = soup.findAll('h1').text
    locsent = location.split()
    loc = str(locsent[3,6]) 

    hightemp = soup.findAll('nobr')[6].text
    htemp = hightemp.split()
    ht = str(htemp[1]) 

    lowtemp = soup.findAll('nobr')[10].text
    ltemp = lowtemp.split()
    lt = str(ltemp[1]) 

    avghum = soup.findAll('td')[23].text

    f.write(loc + "\t|" + ht + "\t|" + lt + "\t|" + avghum + "\n" )

f.close()

但是,我遇到了一个错误,提示是:

Getting data for Location 0
Traceback (most recent call last):
  File "airportweather.py", line 18, in <module>
    location = soup.findAll('H1').text
AttributeError: 'list' object has no attribute 'text'

我查阅了Beautiful Soup和Python的文档,但我还是比较菜,所以没能搞明白。请帮帮我这个新手!

2 个回答

1

这很简单。findAll 会返回一个列表,所以如果你确定你只对一个特定的元素感兴趣,那么可以这样写:soup.findAll('H1')[0].text 这样就可以得到你想要的内容了。

7

.findAll() 方法会返回一个匹配结果的 列表。如果你只想要 一个 结果,可以使用 .find() 方法。或者,你也可以像其他代码那样选择一个特定的元素,或者遍历所有的结果:

location = soup.find('h1').text

或者

locations = [el.text for el in soup.findAll('h1')]

或者

location = soup.findAll('h1')[2].text

撰写回答