- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从 url 中获取 html 内容,并用正则表达式解析 html 内容。但是 html 内容有一些多字节字符。所以我遇到了标题中描述的错误。
谁能告诉我如何解决这个问题?
最佳答案
您需要编辑您的问题以显示 (1) 您使用的代码 (2) 完整的错误和回溯 (3) 涉及的 url (4) 什么是 unicode 字符您正在尝试将 编码 为 gbk
您似乎以某种方式从 html 内容中的原始字节中获取了 unicode 字符——怎么做到的? html内容中指定了什么编码?
然后(我猜)您正在尝试将 unicode 字符写入文件,将 unicode 编码为 gbk。在此过程中,您遇到如下错误:
>>> u'\uffff'.encode('gbk')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'gbk' codec can't encode character u'\uffff' in position 0: illegal multibyte sequence
>>>
如果 html 内容中的原始字节没有用 gbk 编码,那么很可能你有一些不能用 gbk 表示的 unicode 字符。在这种情况下,您可能希望使用原始编码对结果进行编码,或者将它们编码为可以采用任何 unicode 字符的 gb18030。
另一种可能性是您以某种方式破坏了原始字节或 unicode。我当然希望您的正则表达式操作是在 unicode 上完成的,而不是在 gb2312、gbk 等可变长度字符编码上完成的。
更新:
这是您的代码片段:
import sys, urllib.request
url = "http://www.meilishuo.com"
wp = urllib.request.urlopen(url)
content = wp.read()
str_content = content.decode('utf-8')
fp = open("web.txt","w")
fp.write(str_content)
fp.close()
据此我不得不推断:
(1) 你正在运行 Python 3.x
(2) sys.defaultencoding == "gbk"-- 否则您不会收到您之前报告的部分错误消息。
由于我的 sys.defaultencoding 不是“gbk”,我用 gbk_content = str_content.encode('gbk')
替换了你的最后 3 行,并使用 Python 3.1.2 运行修改后的代码段。
观察:
(1)网站有charset=utf-8,用utf-8解码OK
(2) 错误信息:UnicodeEncodeError: 'gbk' codec can't encode character '\u2764' in position 35070: illegal multibyte sequence
\u2664
是一个装饰符号(重黑心)。该网站是动态的;在另一次尝试中,第一个违规字符是\xa9(COPYRIGHT SIGN)。
因此该网页包含未在 gbk 中映射的 Unicode 字符。选项是
(1) 使用 'gbk' 编码但使用 'replace' 选项
(2) 使用 'gbk' 编码但使用 'ignore' 选项
(3) 使用支持所有 Unicode 字符(utf-8、gb18030)的编码进行编码,并且您有一个显示机制可以呈现所有不在 gbk 中的字符
关于python - 统一码编码错误 : 'gbk' codec can't encode character: illegal multibyte sequence,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3218014/
我使用术语“词法编码”是因为我没有更好的编码。 与字母相反,单词可以说是交流的基本单位。 Unicode 尝试为所有已知字母表的每个字母分配一个数值。对一种语言来说是字母,对另一种语言来说是字形。 U
我在UTF-8中有csv文件,我想将其保存在西里尔字母(Windows 1251)中...在中,我仅找到Atom -重新打开,并使用ctrl+shift+u编码 在 Sublime Text 3 中,
在lua 5.3引用手册中,我们可以看到: Lua is also encoding-agnostic; it makes no assumptions about the contents of a
看完后how gzip compression works它让我思考。如果源和代理服务器 (CDN) 都支持 gzip,则添加 Vary: Accept-Encoding头需要吗? 最佳答案 Vary
我正在向我的项目添加一项功能,我们将生成指向我们网站内部内容的链接,并且我们希望这些链接尽可能短,因此我们将制作自己的“URL 缩短器”。 我想知道生成的短网址的最佳编码/字母表是什么。这很大程度上是
我构建了一个用于压缩 HTTP 输出的模块。阅读spec ,我在以下几件事上没有发现明显的区别: 接受编码: 是否应将其视为与 Accept-Encoding: * 相同,还是视为不存在 header
在下面的代码中: package main import ( "bytes" "encoding/json" "fmt" ) type Student struct {
这个问题在这里已经有了答案: Why does encode delete the argument? (1 个回答) 6年前关闭。 Encode::encode 的文档说: encode $octe
在Android4.1中,实时编码应用中经常会请求关键帧。但是如何使用 MediaCodec 对象呢?当前的 Android4.2 SDK 似乎不支持它。 最佳答案 您可以 通过在排队输入缓冲区时指定
我有 CSV 格式的数据,这些数据在字符编码方面被严重打乱,可能在不同的软件应用程序(LibreOffice Calc、Microsoft、Excel、Google Refine、自定义 PHP/My
您可能知道,在 Perl 中,“utf8”意味着 Perl 对 UTF-8 的宽松理解,它允许使用技术上不是 UTF-8 中有效代码点的字符。相比之下,“UTF-8”(或“utf-8”)是 Perl
本文整理了Java中org.geotools.ysld.encode.YsldEncoder.encode()方法的一些代码示例,展示了YsldEncoder.encode()的具体用法。这些代码示例
现在还没有任何关于红色的书,因为它太新了。因此,我正在尝试遵循一本旧的 Rebol 书,并从中挽救我能得到的东西。 我发现一些命令,例如 read,由于文件编码的原因,我无法执行代码。 save %
错误:无法映射用于编码 UTF-8 的字符。由于版权特征,我收到此错误。我使用的是 Netbeans 7.2。 /** * � 2006 * * This class was generate
现在还没有任何关于红色的书,因为它太新了。因此,我正在尝试遵循一本旧的 Rebol 书,并从中挽救我能得到的东西。 我发现一些命令,例如 read,由于文件编码的原因,我无法执行代码。 save %
错误:无法映射用于编码 UTF-8 的字符。由于版权特征,我收到此错误。我使用的是 Netbeans 7.2。 /** * � 2006 * * This class was generate
我正在尝试使用客户端提供的值在 PHP 中测试 Soap Security header 。 他们提供的值(value)如... wTAmCL9tmg6KNpeAQOYubw== ...并说这是一个
这个问题已经有答案了: ClassNotFoundException/NoClassDefFoundError in my Java web application (3 个回答) 已关闭 8 年前。
世界!我正在使用 .Net Framework 4 System.Net.Sockets.TcpClient 编写简单的 HTML 服务器。 我在 StringBuilder html 中有 HTML
我正在尝试使用 Yii 来提供网络服务。自动生成的 wsdl 如下。我可以从命令行成功使用 Web 服务,但是通过 Web 浏览器,我得到了 SOAP-ERROR: Encoding: Violati
我是一名优秀的程序员,十分优秀!