beautifulsoup “list object has no attribute” error

人走茶凉 提交于 2019-12-30 06:56:18

问题


I'm trying to scrape temperatures from a weather site using the following:

    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()

Unfortunately, I get an error saying:

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'

I've looked through BS and Python documentation, but am still pretty green, so I couldn't figure it out. Please help this newbie!


回答1:


The .findAll() method returns a list of matches. If you wanted one result, use the .find() method instead. Alternatively, pick out a specific element like the rest of the code does, or loop over the results:

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

or

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

or

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



回答2:


This is quite simple. findAll returns list, so if you are sure that there is only one interesting you element then: soup.findAll('H1')[0].text should work



来源:https://stackoverflow.com/questions/15324040/beautifulsoup-list-object-has-no-attribute-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!