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

相关文章

使用Pandas将inf, nan转化成特定的值

使用Pandas将inf, nan转化成特定的值

1. 数据处理中很恶心,出现 RuntimeWarning: divide by zero encountered in divide 发现自己的DataFrame中有除以0的运算,出...

Win10下python 2.7.13 安装配置方法图文教程

Win10下python 2.7.13 安装配置方法图文教程

本文记录了Windows10安装Python2.7的详细步骤,分享给大家。 一、下载软件 Python的官方地址 点击Downloads找到进行下载 点击进行下载、有18M左右 下...

python用列表生成式写嵌套循环的方法

将两个嵌套for循环写成一个列表生成式 如,有一个嵌套列表,a=[[1,2],[3,4],[5,6]],要提取列表里的每一个元素 用for循环处理: for i in a: fo...

python基础教程之python消息摘要算法使用示例

python基础教程之python消息摘要算法使用示例

复制代码 代码如下:#! /usr/bin/python'''      File    &nb...

python操作excel让工作自动化

某局某领导给了3只excel文件,一只里面有4个sheet需要处理,一个sheet有250+列,算下来总共有3000+列需要手动反复插入、删除列、拷贝、求和,所以给了4天的时间要完成。...