Trying to save scraped weather data into mysql database using python but given an error because of the degree symbol, anyone know how to get this to work?
My code is;
import urllib2
import MySQLdb
from BeautifulSoup import BeautifulSoup
db = MySQLdb.connect("127.0.0.1","root","","weathersystem")
cursor = db.cursor()
sql = "TRUNCATE TABLE AMSTERDAM "
cursor.execute(sql)
db.commit()
db.close
soup = BeautifulSoup(urllib2.urlopen('http://weather.uk.msn.com/tenday.aspx? wealocations=wc:NLXX0002').read())
for row in soup('div', {'class': 'weadetailed'})[0]('li'):
tds = row('div')
print tds[2].text, tds[3].text, (tds[6].span.text), tds[7].span.text, tds[8].text, tds[9].text
cursor = db.cursor()
sql = "INSERT INTO AMSTERDAM(DAY, DATE, HIGH, LOW, WIND, HUMIDITY) VALUES (%s,%s,%s,%s,%s,%s)"
results = (str(tds[2].text), str(tds[3].text), str(tds[6].span.text),
str(tds[7].span.text), str(tds[8].text), str(tds[9].text))
cursor.execute(sql, results)
db.commit()
db.rollback()
db.close()
And then i am given this error,
Traceback (most recent call last): Today 14 Feb 9° 5° Wind 18 mph SW Humidity 74% File "C:/Users/owner/PycharmProjects/WeatherStation/Innovation Scraper.py", line 18, in results = (str(tds[2].text), str(tds[3].text), str(tds[6].span.text), UnicodeEncodeError: 'ascii' codec can't encode character u'\xb0' in position 1: ordinal not in range(128)