- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
尝试使用 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/
我需要创建一个脚本来发出 HTTP 请求并模拟浏览器在 cookie 管理方面的行为。这意味着它可以访问该“页面”设置的所有 cookie(服务器设置 cookie、异步客户端设置 cookie、每个
我有一个简单的 JavaScript 抓取工具,用于本地页面。 我会找到所有“title”类元素的 id 属性。 问题是,当我在屏幕上打印变量时,变量(title1)永远是“未定义”,而不是像它必须的
我编写了一些代码来解析来自 yell.com 的不同商店的名称、地址和电话号码。如果为我的爬虫提供了任何链接,它就会解析整个内容,而不管它分布在多少页面上。然而,我能发现的唯一问题是它总是跳过第一页的
我用 python 结合 BeautifulSoup 编写了一个脚本,使用其分页按钮转到网站的下一页 (有一个链接连接到这个按钮)直到没有新的页面可供抓取。我的脚本可以使用分页链接抓取下一页。然而,问
我用 python 编写了一个脚本,用于从 craigslist 中抓取五个项目的“姓名”和“电话”。我面临的问题是,当我运行脚本时,它只给出三个结果而不是五个结果。更具体地说,由于前两个链接的页面中
我用 python 结合 selenium 编写了一个脚本来解析网页中的名称。该网站的数据未启用 JavaScript。然而,下一页链接是在 javascript 内的。由于如果我使用 request
我在 python 中编写了一个小脚本,使用 xpath 从 yahoo finance 中抓取显示在左侧区域的标题。该页面中有几个标题,但是当我运行我的脚本时,我只得到三个标题。我不想在这种情况下使
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以
我用 python 结合 Selenium 编写了一个脚本,以提取 finance.yahoo 网站左侧栏中显示的不同新闻的标题。我已经使用 css 选择器来获取内容。但是,该脚本既没有给出任何结果也
我用 python 结合 selenium 编写了一个脚本,以便在搜索时显示一些名称和相应的地址,搜索关键字是“Saskatoon”。但是,在这种情况下,数据会遍历多个页面。除了一件事,我的脚本几乎完
例如,我有下一张 table First 1 Second 2
想要在某些 channel 统计数据中删除youtube,并了解其工作原理。并且比作csv作以下分析。使用this video创建和学习。 我有2个文件: main.py,youtube_statis
我正在尝试抓取此页面 https://www.teamrankings.com/nba/team/cleveland-cavaliers但我需要当您单击“投注 View ”时显示的网格...我遇到了一
我编写了一个抓取工具来解析来自 torrent 站点的电影信息。我使用了 IE 和 queryselector。 我的代码确实解析了所有内容。当一切完成后,它会抛出错误而不是退出浏览器。如果我取消错误
我正在尝试创建一个在名为 https://en.wikipedia.org/wiki/North_Korea_and_weapons_of_mass_destruction 的维基百科页面上启动的蜘蛛
我正在尝试抓取this网站使用 python 为献血营准备数据库。 首先,在尝试从 requests 或 urllib 获取网站 html 源代码时,存在 SSl:certificate_verify
我正在尝试使用允许空格和句点的正则表达式编写一个 python scraper。 我正在尝试从某个网站获取棒球队名称,因此有些是 Miami (全字),但其他是 San Francisco (带空格)
我已经用Python创建了一个网络爬虫,但是在最后打印时我想打印我已经下载的(“Bakerloo:”+ info_from_website),正如你在代码中看到的那样,但它总是像info_from_w
我有这个代码: #!/opt/local/bin/perl use 5.014; use warnings; use Web::Scraper; use Data::Dumper; my $html
我正在尝试使用 Scrapy 来抓取该网站。 首先这是我的代码-: from twisted.internet import reactor from scrapy.crawler import Cr
我是一名优秀的程序员,十分优秀!