Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析HTML时Python Unicode和ASCII问题_Python_Html_Unicode_Encoding_Utf 8 - Fatal编程技术网

解析HTML时Python Unicode和ASCII问题

解析HTML时Python Unicode和ASCII问题,python,html,unicode,encoding,utf-8,Python,Html,Unicode,Encoding,Utf 8,我正在尝试编写一个Python脚本,其作用类似于Chrome web浏览器上的Ctrl+S,它保存HTML页面,下载网页上的任何链接,最后用磁盘上的本地路径替换链接的URI 下面的代码试图用我的计算机上的本地路径替换CSS文件中的URI 我在尝试解析不同的站点时遇到了一个问题,这让我有点头疼 我的原始错误代码是UnicodeDecodeError:“ascii”编解码器无法对13801位置的字节0xa3进行解码:序号不在范围(128) 当我尝试打印新内容或将其写入文件时,此问题仍然存在。我试图按

我正在尝试编写一个Python脚本,其作用类似于Chrome web浏览器上的Ctrl+S,它保存HTML页面,下载网页上的任何链接,最后用磁盘上的本地路径替换链接的URI

下面的代码试图用我的计算机上的本地路径替换CSS文件中的URI

我在尝试解析不同的站点时遇到了一个问题,这让我有点头疼

我的原始错误代码是
UnicodeDecodeError:“ascii”编解码器无法对13801位置的字节0xa3进行解码:序号不在范围(128)

当我尝试打印新内容或将其写入文件时,此问题仍然存在。我试图按照这道题的最上面的答案来回答,并修改了我的行

newContent = webContent.decode('utf-8').replace(cssUri, "./" + title + '/' + cssFilename)
newContent=webContent.decode(utf-8)。替换(cssUri,“./”+title+'/'+cssFilename)
。我还尝试了
.decode(utf-16)
和32,分别得到了以下错误代码:
13801:无效的开始字节
位置44442的字节0x0a:截断的数据
,最后
无法解码位置0-3的字节:代码点不在范围内(0x110000)

有人知道我应该如何解决这个问题吗?我必须补充一点,当我打印变量webContent时,有输出(我注意到底部有中文书写)

这会解决你的问题 使用
webContent.decode('utf-8',errors='ignore')
webContent.decode('latin-1')

webContent[13801:13850]
有一些奇怪的字符。别理他们

忽略下面的一切
这有点像是在黑暗中拍摄的,但是试试这个:

在您的文件顶部

from __future__ import unicode_literals
from builtins import str
现在发生的事情似乎是,您正在尝试解码一个python对象,该对象可能是Python2.7
str
对象,原则上应该是一些已解码的文本对象

简要说明 在默认的python 2.7内核中:

(伊皮顿会议)

python 2.7中需要注意的一些其他事项:

  • “é”
    str(“é”)
  • u“é”
    “é”相同。解码('utf-8')
    unicode(“é”,'utf-8')
  • u“é”。encode('utf-8')
    str(“é”)
  • 通常使用py2
    str调用decode,并使用py2
    unicode
    编码。
    • 由于早期的设计问题,您可以调用这两个选项中的任何一个,尽管这实际上没有任何意义
    • 在python3中,
      str
      ,与python2
      unicode
      相同,不能再被解码,因为根据定义,字符串是已解码的字节序列。默认情况下,它使用utf-8编码
  • 在ascii编解码器中使用进行编码的字节序列的行为与解码的字节序列的行为完全相同。
    • 在没有未来导入的python 2.7中:
      type(“a”).decode('ascii')
      提供了一个unicode对象,但其行为与
      str(“a”)
      几乎相同。python3的情况并非如此
话虽如此,以上代码片段的作用如下:

  • \uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
  • from\uuuuu future\uuuuu导入unicode\u文本
    具有以下效果:
    • 没有将来的导入,
      “é”
      str(“é”)
    • 随着未来的导入,
      “é”
      在功能上与
      unicode(“é”)
  • builtins
    是一个由核心python团队批准的模块,它包含用于在python2和python3 api中使用python3习惯用法的安全别名。
    • 由于我无法理解的原因,包本身被命名为“future”,因此要安装运行的
      内置模块:
      pip install future
  • 来自内置导入str的
    具有以下效果:
    
    • str
      构造函数现在提供了您认为它的功能,即python2 unicode对象形式的文本数据。因此,它在功能上与
      str=unicode
    • 注意:Python3
      str
      在功能上与Python2
      unicode
    • 注意:要获取字节,可以使用“bytes”前缀,例如
      b'é'
外卖是这样的:

  • 读取时解码/早期解码,最后写入时编码/编码
  • 使用
    str
    对象表示字节,使用
    unicode
    对象表示文本

  • 我建议您观看和阅读,特别注意“Unicode三明治”。您是使用python2还是3?@ThomasTu python2.7,我也会看一看Unicode三明治文章
    from __future__ import unicode_literals
    from builtins import str
    
    In [1]: type("é") # By default, quotes in py2 create py2 strings, which is the same thing as a sequence of bytes that given some encoding, can be decoded to a character in that encoding.
    Out[1]: str
    
    In [2]: type("é".decode("utf-8")) # We can get to the actual text data by decoding it if we know what encoding it was initially encoded in, utf-8 is a safe guess in almost every country but Myanmar.
    Out[2]: unicode
    
    In [3]: len("é") # Note that the py2 `str` representation has a length of 2.  There's one byte for the "e" and one byte for the accent.  
    Out[3]: 2
    
    In [4]: len("é".decode("utf-8")) # the py2 `unicode` representation has length 1, since an accented e is a single character
    Out[4]: 1