Windows8下安装Python的BeautifulSoup

yipeiwu_com5年前Python基础

运行环境:Windows 8.1
Python:2.7.6

在安装的时候,我使用的pip来进行安装,命令如下:

复制代码 代码如下:

pip install beautifulsoup4

运行的时候,报错如下:
复制代码 代码如下:

Exception:
Traceback (most recent call last):
  File "J:\Program Files (x86)\Python\Python27\lib\site-packages\pip\basecomm
.py", line 122, in main
    status = self.run(options, args)
  File "J:\Program Files (x86)\Python\Python27\lib\site-packages\pip\commands
stall.py", line 278, in run
    requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bu
e=self.bundle)
  File "J:\Program Files (x86)\Python\Python27\lib\site-packages\pip\req.py",
ne 1229, in prepare_files
    req_to_install.run_egg_info()
  File "J:\Program Files (x86)\Python\Python27\lib\site-packages\pip\req.py",
ne 292, in run_egg_info
    logger.notify('Running setup.py (path:%s) egg_info for package %s' % (sel
etup_py, self.name))
  File "J:\Program Files (x86)\Python\Python27\lib\site-packages\pip\req.py",
ne 265, in setup_py
    import setuptools
  File "build\bdist.win-amd64\egg\setuptools\__init__.py", line 11, in <modul
    from setuptools.extension import Extension
  File "build\bdist.win-amd64\egg\setuptools\extension.py", line 5, in <modul
  File "build\bdist.win-amd64\egg\setuptools\dist.py", line 15, in <module>
  File "build\bdist.win-amd64\egg\setuptools\compat.py", line 19, in <module>
  File "J:\Program Files (x86)\Python\Python27\lib\SimpleHTTPServer.py", line
, in <module>
    class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  File "J:\Program Files (x86)\Python\Python27\lib\SimpleHTTPServer.py", line
8, in SimpleHTTPRequestHandler
    mimetypes.init() # try to read system mime.types
  File "J:\Program Files (x86)\Python\Python27\lib\mimetypes.py", line 358, i
nit
    db.read_windows_registry()
  File "J:\Program Files (x86)\Python\Python27\lib\mimetypes.py", line 258, i
ead_windows_registry
    for subkeyname in enum_types(hkcr):
  File "J:\Program Files (x86)\Python\Python27\lib\mimetypes.py", line 249, i
num_types
    ctype = ctype.encode(default_encoding) # omit in 3.x!
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb0 in position 1: ordin
not in range(128)

Storing debug log for failure in C:\Users\Administrator\pip\pip.log

解决方法:打开C:\Python27\Lib下的 mimetypes.py 文件,找到大概256行的

复制代码 代码如下:

default_encoding = sys.getdefaultencoding()

改成:
复制代码 代码如下:

if sys.getdefaultencoding() != 'gbk':
    reload(sys)
    sys.setdefaultencoding('gbk')
default_encoding = sys.getdefaultencoding()

安装成功后,验证是否安装成功:

复制代码 代码如下:

C:\Users\Administrator>python
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on 32
Type "help", "copyright", "credits" or "license" for more information.
>>> from bs4 import BeautifulSoup
>>> exit()

如果“from bs4 import BeautifulSoup”没有报错的话,则说明安装成功,否则,将报类似错误如下:
复制代码 代码如下:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named bs4

相关文章

python进程类subprocess的一些操作方法例子

subprocess.Popen用来创建子进程。 1)Popen启动新的进程与父进程并行执行,默认父进程不等待新进程结束。 复制代码 代码如下: def TestPopen(): &nb...

Python-OpenCV基本操作方法详解

Python-OpenCV基本操作方法详解

基本属性 cv2.imread(文件名,属性) 读入图像 属性:指定图像用哪种方式读取文件 cv2.IMREAD_COLOR:读入彩色图像,默认参数,Opencv 读取彩色图像为BGR...

解决python执行不输出系统命令弹框的问题

最近做一个的GUI,因为调用了os模块里的system方法,使用pyinstaller打包的时候选择不输出系统命令弹框,程序无法运行,要求要有系统命令框。在网上找到一个解决办法。使用su...

浅谈Python生成器generator之next和send的运行流程(详解)

对于普通的生成器,第一个next调用,相当于启动生成器,会从生成器函数的第一行代码开始执行,直到第一次执行完yield语句(第4行)后,跳出生成器函数。 然后第二个next调用,进入生成...

python获取命令行输入参数列表的实例代码

(一)单一独立的参数 如果命令行输入的参数都是各自单一独立的,直接用个循环把所有参数逐一读出来就行了。sys模块里面直接用args = sys.argv就可以获取到所有参数了(返回值ar...