Python|Python - BeautifulSoup4安装的艰辛历程

想安装一个网页抓取数据的Python第三方库——BeautifulSoup4,但殊不知路途艰辛……
这里,作者是在Windows下(Python 2.7)使用pip来安装的
首先,下载pip1.5.4
点击下载
(推荐用迅雷极速版,试过其他浏览器下载,太慢而且不稳定)

Python|Python - BeautifulSoup4安装的艰辛历程
文章图片
下载
之后,解压文件
  • 先使用cmd命令转到解压文件后文件目录
    比如 cd C:\Python27\pip-1.5.4
  • 再运行 setup.py build
  • 再运行 setup.py install
(这个时候安装可能会提示没有安装settools,那么读者应该下载settools,然后像安装pip一样安装settools)
之后再在系统环境变量(pash)中添加 C:\Python27\; C:\Python27\Scripts;
Python|Python - BeautifulSoup4安装的艰辛历程
文章图片
添加环境变量 最后便可以直接在cmd中调用pip
pip
控制台输出:
Usage: pip [options]Commands: installInstall packages. uninstallUninstall packages. freezeOutput installed packages in requirements format listList installed packages. showShow information about installed packages. searchSearch PyPI for packages. wheelBuild wheels from your requirements. zipDEPRECATED. Zip individual packages. unzipDEPRECATED. Unzip individual packages. bundleDEPRECATED. Create pybundles. helpShow help for commands.General Options: -h, --helpShow help. -v, --verboseGive more output. Option is additive, and can be used up to 3 times. -V, --versionShow version and exit. -q, --quietGive less output. --log-file Path to a verbose non-appending log, that only logs failures. This log is active by default at C:\Users\杨骐嘉\pip\pip.log. --log Path to a verbose appending log. This log is inactive by default. --proxy Specify a proxy in the form [user:passwd@]proxy.server:port. --timeoutSet the socket timeout (default 15 seconds). --exists-actionDefault action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup. --cert Path to alternate CA bundle.

作者的pip在这个时候突然报错
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb1 in position 34: ordinalnot in range(128)

原因是pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码。解决办法是: python目录 Python27\Lib\site-packages 建一个文件sitecustomize.py 内容写:
import sys sys.setdefaultencoding('gb2312')

python会自动运行这个文件。来源
最后的最后,使用pip安装BeautifulSoup4
pip install beautifulsoup4
在Python中,导入模块成功!
from bs4 import BeautifulSoup
【Python|Python - BeautifulSoup4安装的艰辛历程】温馨提示(pip常用命令):
#安装包 pip install xxx #升级包,可以使用-U 或者 --upgrade pip install -U xxx #卸载包 pip uninstall xxx #列出已安装的包 pip list

    推荐阅读