gpt4 book ai didi

Python Scraper无法在mysql数据库中保存度数符号°

转载 作者:行者123 更新时间:2023-11-29 08:10:07 24 4
gpt4 key购买 nike

尝试使用 python 将抓取的天气数据保存到 mysql 数据库中,但由于度数符号而出现错误,有人知道如何让它工作吗?

我的代码是;

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

然后我得到了这个错误,

回溯(最近一次调用最后一次):今天 2 月 14 日 9° 5° 风速 18 mph SW 湿度 74% 文件“C:/Users/owner/PycharmProjects/WeatherStation/Innovation Scraper.py”,第 18 行,位于 结果 = (str(tds[2].text), str(tds[3].text), str(tds[6].span.text),UnicodeEncodeError:“ascii”编解码器无法对位置 1 中的字符 u'\xb0' 进行编码:序号不在范围内(128)

最佳答案

回溯表明 BeautifulSoup 或 Python 安装有问题。看看their documentation :

If you're getting errors that say: "'ascii' codec can't encode character 'x' in position y: ordinal not in range(128)", the problem is probably with your Python installation rather than with Beautiful Soup. Try printing out the non-ASCII characters without running them through Beautiful Soup and you should have the same problem. For instance, try running these three lines of code like this:

>>> latin1word = 'Sacr\xe9 bleu!'
>>> unicodeword = unicode(latin1word, 'latin-1')
>>> print unicodeword
Sacré bleu!

(请注意,这应该在交互式解释器中,而不是在脚本中。在脚本中,如果将其粘在底部,您仍然会收到该错误。)

如果有效(即您看到返回的最后一行),则问题出在 BeautifulSoup 中,是的,您应该升级到 bs4。如果该打印行输出回溯,则问题出在您的 Python 安装中。可以在上面的链接中找到解决该问题的说明。

另一方面,MySQLdb 默认使用 latin1 字符集。除非包含 kwarg charset='utf8',否则您将无法将该 Unicode 数据插入表中:

db = MySQLdb.connect("127.0.0.1","root","","weathersystem", charset="utf8")

关于Python Scraper无法在mysql数据库中保存度数符号°,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21769292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com