Reputation: 11304
I'm new at BeautifulSoup and have scoured the online docos but couldn't find what I was after, I just need to store the value of a tag into a variable so that it can be written to a file afterwards
Here is the code:
import urllib2
from BeautifulSoup import BeautifulSoup
f = open('C:\test.txt', 'w')
url = "http://www.wunderground.com/history/airport/KBUF/2011/1/1/DailyHistory.html?MR=1"
page = urllib2.urlopen(url)
soup = BeautifulSoup(page)
table = soup.find('table', id="historyTable").tbody
rows = table.findAll('tr') #get all rows
cols = rows[1].findAll('td') #get 2nd row
meanTemp = cols[1] # get 2nd column
alt = meanTemp('span')[1]
print alt
dayTemp = alt.contents
print dayTemp
f.write(timestamp + ',' + dayTemp + '\n')
It prints this:
<span class="b">8</span>
[u'8']
and then gives this error:
f.write(timestamp + ',' + dayTemp + '\n')
TypeError: cannot concatenate 'str' and 'list' objects
i.e.: I would like the print dayTemp
line to actually print 8
instead of [u'8']
Upvotes: 0
Views: 1507
Reputation: 36777
To add to previous answers if you're not sure about the contents of your span tag you can use:
f.write(timestamp + ',' + '<sep>'.join(dayTemp) + '\n')
Where <sep> is a separator of your choice.
Upvotes: 1
Reputation: 123772
The problem is that [u'8']
is a list
containing one object, a Unicode string. If you want to get the (only) object in that list, you index it to get its first entry:
[u'8'][0] # is u'8'
or you can pattern-match its value out:
[a] = [u'8'] # now a is u'8'
Upvotes: 1
Reputation: 14830
By your error it seems the solution is:
f.write(timestamp + ',' + dayTemp[0] + '\n')
Upvotes: 1