2013-05-26 51 views
2
解析与BeautifulSoup4,chardet的和Python 3.3的网页

我收到以下错误,当我尝试调用BeautifulSoup(页)错误而在Windows

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' 

我正在运行的Python 3.3在Windows 7中,我已经安装了通过下载.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

我刚才符合这种情况。
不要导入chardet,我也卸载chardet。
然后构建会通过。
下面的代码是美丽的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