python - beautifulsoup "list object has no attribute"错误

标签 python web-scraping beautifulsoup

我正在尝试从 weather site 中获取温度信息使用以下内容:

    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'

我已经查看了 BS 和 Python 文档,但仍然很新手,所以我无法弄清楚。请帮助这个新手!

最佳答案

.findAll() 方法返回匹配项的列表。如果您想要一个 结果,请改用.find() 方法。或者,像其余代码一样挑选特定元素,或循环结果:

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

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

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

关于python - beautifulsoup "list object has no attribute"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15324040/

相关文章:

python - 解析 HTML、Python 中特定标签下的文本

python - 如何在 Windows 7 上安装 SIP 和 PyQT

python - QGraphicsScene 鼠标事件

javascript - JS 网页抓取找不到某些元素

python - BeautifulSoup 刮电影标题和图像

python - BeautifulSoup: AttributeError: 'str' 对象没有属性 'copy' 。在 Ubuntu 上工作,在 Windows 上失败

python - .contents 和 .children 之间的区别

python - 如何将文件从 FileField 复制到另一个具有不同上传路径 Django 的 FileField?

python - 如何将标签添加到 pandas dataframe.to_html 链接,以便 url 的绝对路径不会显示在 html 中,而是显示在标签中?

asp.net - 抓取 : SSL_ERROR_SYSCALL with cURL. 在 Chrome/Firefox 中有效