想安装一个网页抓取数据的Python第三方库——BeautifulSoup4,但殊不知路途艰辛……
这里,作者是在Windows下(Python 2.7)使用pip来安装的
首先,下载pip1.5.4
点击下载
(推荐用迅雷极速版,试过其他浏览器下载,太慢而且不稳定)
![](https://img.haomeiwen.com/i2832391/84eb212a247171af.png)
之后,解压文件
- 先使用cmd命令转到解压文件后文件目录
比如cd C:\Python27\pip-1.5.4
- 再运行
setup.py build
- 再运行
setup.py install
(这个时候安装可能会提示没有安装settools,那么读者应该下载settools,然后像安装pip一样安装settools)
之后再在系统环境变量(pash)中添加 C:\Python27\;C:\Python27\Scripts;
![](https://img.haomeiwen.com/i2832391/a5e3521635c1b051.png)
最后便可以直接在cmd中调用pip
pip
控制台输出:
Usage:
pip <command> [options]
Commands:
install Install packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format
list List installed packages.
show Show information about installed packages.
search Search PyPI for packages.
wheel Build wheels from your requirements.
zip DEPRECATED. Zip individual packages.
unzip DEPRECATED. Unzip individual packages.
bundle DEPRECATED. Create pybundles.
help Show help for commands.
General Options:
-h, --help Show help.
-v, --verbose Give more output. Option is additive, and can be
used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output.
--log-file <path> 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> Path to a verbose appending log. This log is
inactive by default.
--proxy <proxy> Specify a proxy in the form
[user:passwd@]proxy.server:port.
--timeout <sec> Set the socket timeout (default 15 seconds).
--exists-action <action> Default action when a path already exists:
(s)witch, (i)gnore, (w)ipe, (b)ackup.
--cert <path> 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
温馨提示(pip常用命令):
#安装包
pip install xxx
#升级包,可以使用-U 或者 --upgrade
pip install -U xxx
#卸载包
pip uninstall xxx
#列出已安装的包
pip list
网友评论