Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

beautifulsoup "list object has no attribute" error

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!

like image 231
Brad Avatar asked Mar 10 '13 15:03

Brad


2 Answers

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
like image 183
Martijn Pieters Avatar answered Nov 12 '22 10:11

Martijn Pieters


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

like image 1
kkonrad Avatar answered Nov 12 '22 09:11

kkonrad