- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我想做搜索引擎,我在一些网络上学习教程。我想测试解析 html
from bs4 import BeautifulSoup
def parse_html(filename):
"""Extract the Author, Title and Text from a HTML file
which was produced by pdftotext with the option -htmlmeta."""
with open(filename) as infile:
html = BeautifulSoup(infile, "html.parser", from_encoding='utf-8')
d = {'text': html.pre.text}
if html.title is not None:
d['title'] = html.title.text
for meta in html.findAll('meta'):
try:
if meta['name'] in ('Author', 'Title'):
d[meta['name'].lower()] = meta['content']
except KeyError:
continue
return d
parse_html("C:\\pdf\\pydf\\data\\muellner2011.html")
然后出现错误
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 867: character maps to <undefined>enter code here
我在网上看到了一些使用 encode() 的解决方案。但我不知道如何在代码中插入 encode() 函数。谁能帮帮我?
最佳答案
在 Python 3 中,文件以文本形式打开(解码为 Unicode);您无需告诉 BeautifulSoup 解码的编解码器。
如果数据解码失败,那是因为你没有告诉 open()
在读取文件时调用什么编解码器;使用 encoding
参数添加正确的编解码器:
with open(filename, encoding='utf8') as infile:
html = BeautifulSoup(infile, "html.parser")
否则文件将使用您的系统默认编解码器打开,这取决于操作系统。
关于Python 3 UnicodeDecodeError : 'charmap' codec can't decode byte 0x9d,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30750843/
我有一个文本文件,发布者(美国证券交易委员会)声称该文件以 UTF-8 编码(https://www.sec.gov/files/aqfs.pdf,第 4 节)。我正在使用以下代码处理这些行: def
在 django 界面中添加元素时遇到问题。我有两个定义: # -*- coding: utf-8 -*- class VisitType(models.Model): name=models
我尝试制作一个脚本来使用 Mechanize 发布表单 剧本: # Browser br = mechanize.Browser() cj = cookielib.LWPCookieJar() br.
我收到此错误: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 4: ordinal not in range
我正在尝试使用 Google 语音 API 在 Python 中制作语音识别器。我一直在使用和改编 here 中的代码(转换为Python3)。我在计算机上使用一个音频文件,该文件已使用在线转换器从
打开一个docker实例(例如docker run -ti ubuntu:16.04),创建Python文件a.py # -*- coding: utf-8 -*- a = 'ö' 和r.py wit
当我将应用程序与Buildozer for Android打包在一起时,我会收到UnicodeDecodeError。 Log2与Buildozer一起附加 build.py 。 作业系统:UBUNT
我在 Ubuntu 终端(编码设置为 utf-8)中运行此代码段两次,一次使用 ./test.py然后用 ./test.py >out.txt : uni = u"\u001A\u0BC3\u1451
我正在尝试使用 Python 中的以下命令序列替换 Word 文件中的子字符串。代码本身工作得很好 - 即使使用完全相同的 Word 文件,但当将其嵌入到更大规模的项目结构中时,它会在确切的位置抛出错
我在 tox 中有以下配置: [tox] envlist = py37 [testenv] passenv = TRAVIS TRAVIS_* setenv = DEFAULT_FROM =
我正在获取 UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 104: ordinal not in range
在执行子字符串匹配时,我收到 UnicodeDecodeError: 'ascii' codec can't Decode byte 0xc3 inposition 8: ordinal not in
我正在使用 Python 和 lxml,但遇到错误 我的代码 >>>import urllib >>>from lxml import html >>>response = urllib.urlope
我是 python 的新手,我正在尝试处理一小部分 Yelp!数据集是 JSON,但我使用 pandas 库和 NLTK 转换为 CSV。 在对数据进行预处理时,我首先尝试删除所有标点符号以及最常见的
我想不出如何一劳永逸地解决这些问题。当我尝试写“è”(我是意大利人)时,我第一次遇到这些问题。经过一些研究,我发现在最开始添加“#coding: utf-8”似乎可以解决问题....直到现在。 我编辑
我的数据存储包含值,我希望我的表单能够更新这些值。我在 jinja2 中使用 wtforms 和谷歌应用引擎。我收到一条我无法理解的错误消息: 'ascii' codec can't decode b
我们遇到了一个问题(描述为 http://wiki.python.org/moin/UnicodeDecodeError)——请阅读第二段“...自相矛盾...”。 具体来说,我们正在尝试将字符串向上
我正在尝试标记一些文档,但我遇到了这个错误 UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 6: ordina
我想在一个文件中存储一个包含多个 numpy 数组的 Python 对象。我找到了 pickle,但在加载存储的对象时总是遇到 UnicodeDecodeError: Traceback (mos
我正在尝试制作一个 python 脚本来查找 USB 闪存驱动器中的重复文件。 我遵循的过程是创建一个文件名列表,散列每个文件,然后创建一个逆向字典。然而,在过程中的某个地方,我得到了一个 Unico
我是一名优秀的程序员,十分优秀!