python - 在 Windows 中使用 BeautifulSoup4、Chardet 和 Python 3.3 解析页面时出错

标签 python html windows beautifulsoup chardet

当我尝试调用 BeautifulSoup(page) 时出现以下错误

Traceback (most recent call last):
 File "error.py", line 10, in <module>
  soup = BeautifulSoup(page)
 File "C:\Python33\lib\site-packages\bs4\__init__.py", line 169, in __init__
  self.builder.prepare_markup(markup, from_encoding))
 File "C:\Python33\lib\site-packages\bs4\builder\_htmlparser.py", line 136, in
 prepare_markup
  dammit = UnicodeDammit(markup, try_encodings, is_html=True)
 File "C:\Python33\lib\site-packages\bs4\dammit.py", line 223, in __init__
  u = self._convert_from(chardet_dammit(self.markup))
 File "C:\Python33\lib\site-packages\bs4\dammit.py", line 30, in chardet_dammit

   return chardet.detect(s)['encoding']
 File "C:\Python33\lib\site-packages\chardet\__init__.py", line 21, in detect
  import universaldetector
ImportError: No module named 'universaldetector'

我在 Windows 7 中运行 Python 3.3,我通过下载 .tar.gz 从 setup.py 安装了 bs4。我已经安装了 pip,然后通过执行 pip.exe install chardet 安装了 chardet。我的 chardet 版本是 2.2.1。 Bs4 适用于其他网址。

这是代码

import sys
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import chardet

url = "http://www.edgar-online.com/brand/yahoo/search/?cik=1400810"
page = urlopen(url).read()
#print(page)
soup = BeautifulSoup(page)

期待您的回答

最佳答案

我现在遇到这种情况。
不要导入chardet,我也卸载了chardet。
然后构建将通过。
下面的代码是 beautifulsoup 中 dammit.py 库的一部分。
可能是你导入了一个不适合python 3.3的chardet,所以出现这个错误。

try:
    # First try the fast C implementation.
    #  PyPI package: cchardet
    import cchardet
    def chardet_dammit(s):
        return cchardet.detect(s)['encoding']
except ImportError:
    try:
        # Fall back to the pure Python implementation
        #  Debian package: python-chardet
        #  PyPI package: chardet
        import chardet
        def chardet_dammit(s):
            return chardet.detect(s)['encoding']
        #import chardet.constants
        #chardet.constants._debug = 1
    except ImportError:
        # No chardet available.
        def chardet_dammit(s):
            return None

关于python - 在 Windows 中使用 BeautifulSoup4、Chardet 和 Python 3.3 解析页面时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16756842/

相关文章:

windows - 创建目录并通过发出一个 IRP 获取句柄

python - 如何使用 Pandas 从 CSV 文件夹生成自定义主数据框?

html - 在 CSS 中更改边框高度?

c++ - 在Windows 8.1中使用OpenCV在C++中加载图像需要很长时间

javascript - 为什么 Google Chrome 9 不显示来自 Amazon S3 的图像?

javascript - jQuery 平滑滚动禁用窗口位置的类更改

c++ - 通过索引获取文件名

python - 正则表达式在 Python 3 中将文本与分隔符匹配

python - 系统错误 : <built-in function putText> returned NULL without setting an error

python - 没有客户端/服务器的 key : value store in Python for possibly 100 GB of data,