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

时间:2013-05-26 06:44:39

标签: python html windows beautifulsoup chardet

当我尝试调用BeautifulSoup(页面)

时出现以下错误
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)

我期待你的回答

1 个答案:

答案 0 :(得分:1)

我刚才遇到这种情况 不要导入chardet,我也卸载chardet 然后构建将通过。
下面的代码是beautifulsoup中dammit.py lib的一部分 也许您导入的chardet不适合python 3.3,因此会发生错误。

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