【问题标题】:BeautifulSoup HTMLparsingError between Python2 and Python3Python2 和 Python3 之间的 BeautifulSoup HTMLparsingError
【发布时间】:2018-03-05 18:38:27
【问题描述】:

我在 Python27 上运行了一个 bs4 程序,它运行良好,我在使用 Python3 时遇到了问题。我正在为两者使用更新版本的 bs4。我正在运行它的文件是 html,我注意到错误出现在标签上。是否有我需要更新的支持模块?喜欢 lxml?

代码:

from bs4 import BeautifulSoup

data = open(directory +'\\'+ file)
soup = BeautifulSoup(data, 'html.parser')

这是错误:

...
File "C:\Anaconda3\lib\html\parser.py", line 174, in error 
      raise HTMLParseError(message, self.getpos())
html.parser.HTMLParseError: unknown status keyword 'NKXE' in marked section, 
      at line 318, column 49

永远感谢您的帮助!

【问题讨论】:

  • 检查一下你的文件处理程序是否正在读取...
  • 不确定您要什么。你的意思是什么是“数据”对象?因为我在 python27 中看到它是 .
  • 然后在python3它的TextIOWrapper: <_io.textiowrapper name="C:\\Users\\..txt" mode="r" encoding="cp1252">

标签: python html beautifulsoup python-2to3


【解决方案1】:

查看是否安装html5lib

pip install html5lib

然后发出这样的请求可以解决问题。

from bs4 import BeautifulSoup

data = open(directory +'\\'+ file)
soup = BeautifulSoup(data, 'html5lib')

这对我有用。

【讨论】:

  • 仔细查看错误消息,您似乎正在使用 Anaconda,因此您可能需要使用 conda 而不是 pip 来安装 html5lib
  • 我确实使用 conda。再次感谢。
猜你喜欢
  • 2020-09-14
  • 1970-01-01
  • 2015-06-17
  • 2011-06-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-08
  • 1970-01-01
相关资源
最近更新 更多