Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.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
Linux 在Python3中使用HTMLParser解析HTML_Linux_Windows_Parsing_Python 3.x_Html Parsing - Fatal编程技术网

Linux 在Python3中使用HTMLParser解析HTML

Linux 在Python3中使用HTMLParser解析HTML,linux,windows,parsing,python-3.x,html-parsing,Linux,Windows,Parsing,Python 3.x,Html Parsing,我在Python3中有一段代码,它在Windows中使用HTMLParser成功解析HTML,问题是我也想在Linux中运行该脚本,但它似乎不起作用 我使用以下内容检索HTML代码: html = urllib.request.urlopen(url).read() html_str = str(html) parse = MyHTMLParser() parse.feed(html_str) b'\n \n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML

我在Python3中有一段代码,它在Windows中使用HTMLParser成功解析HTML,问题是我也想在Linux中运行该脚本,但它似乎不起作用

我使用以下内容检索HTML代码:

html = urllib.request.urlopen(url).read()
html_str = str(html)
parse = MyHTMLParser()
parse.feed(html_str)
b'\n \n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n
    <html xmlns="http://www.w3.org/1999/xhtml">\n
        <head>\n
html
的原始输出如下:

html = urllib.request.urlopen(url).read()
html_str = str(html)
parse = MyHTMLParser()
parse.feed(html_str)
b'\n \n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"\n
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n
    <html xmlns="http://www.w3.org/1999/xhtml">\n
        <head>\n
正如您所看到的,我有几个
\\n
,Windows对此毫不在意,但对于Linux,它们是转义序列,因此无法解析html。我现在不记得确切的错误了,但有点像
无法解析\\

我曾尝试使用
re
删除
re.sub(“\\”,“”,html\u str)
中多余的
\
,但在Windows中似乎什么也做不了,在Linux中我也遇到了一个错误

这是我在Linux中尝试
re.sub
html时遇到的错误:

>>> re.sub("\\","",html_str)
Traceback (most recent call last):
  File "/usr/lib/python3.1/sre_parse.py", line 194, in __next
    c = self.string[self.index + 1]
IndexError: string index out of range

你知道如何去除
html\u str
中多余的
\
,以便在Linux中解析它吗?

在python3中,你不能像现在这样将
字节
转换成
str

html_str = str(html)
这在python2中起作用,因为
bytes
str
是相同的,但现在您将获得原始字符串的表示形式。要解码字符串,您需要提供参数,或使用:

hmtl_str = html.decode(encoding)

如果无法从http头中获取字符集,可以尝试猜测,或使用来确定正确的编码。

\\n
在Linux上不是转义序列<代码>\\n是两个字符,一个反斜杠(转义为
\\
,使输出成为有效的python字节文字)和一个
n
字符。这些字符在Windows和Linux上具有相同的含义。请您查找准确的错误并进行回溯好吗?请注意,
str(html,'ascii')
html.decode('ascii')
是一样的。