脚本专栏 
首页 > 脚本专栏 > 浏览文章

Windows8下安装Python的BeautifulSoup

(编辑:jimmy 日期: 2024/11/28 浏览:3 次 )

运行环境: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中集合类型(set)学习小结
下一篇:Python中使用装饰器和元编程实现结构体类实例
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网 网站地图 SiteMap