注意我的python的windows安装目录为:D:\Python37-32
python进行爬虫时引用到BeautifuSoup开源的xml解析工具。以下为beautifulsoup4的windows的安装步骤:
首先去网站下载beautifulsoup压缩包(beautifulsoup4-4.6.0.tar.gz)
https://www.crummy.com/software/BeautifulSoup/#Download
将下载下来的压缩包解压后放入到python的安装目录(D:\Python37-32\beautifulsoup4-4.6.0)
进入cmd,执行命令:
- cd D:\Python37-32\beautifulsoup4-4.6.0
- setup.py build
- setup.py install
在命令行输入:python
然后输入:from bs4 import BeautifulSoup
如果不出现任何提示信息表示安装成功。如果如下所示则安装失败,版本不兼容
在python3中引用BeautifuSoup4时会出现版本不兼容的情况,具体看下面的错误代码
- >>> from bs4 import BeautiulSoup
- Traceback (most recent call last):
- File "<stdin>", line 1, in <module>
- File "D:\Python37-32\beautifulsoup4-4.6.0\bs4\__init__.py", line 53
- 'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.'<>'You need to convert the code, either by installing it (`python setup.py install`) or by running 2to3 (`2to3 -w bs4`).'
- ^
- SyntaxError: invalid syntax
将beautifulSoup解压目录中的bs4文件夹( D:\Python37-32\beautifulsoup4-4.6.0\bs4 )和 2to3.py( D:\Python37-32\Tools\scripts\ )复制到python的安装目录下的Lib( D:\Python37-32\Lib )文件夹下
进入D:\Python37-32\Lib 目录,并执行 2to3.py bs4 -w 命令
- cd D:\Python37-32\Lib
- 2to3.py bs4 -w
然后输入 from bs4 import BeautifulSoup