下载
由于需要用到beautifulsoup,因此首先去下载:下载地址
安装
然后解压缩到D:/python目录下,打开cmd窗口,进入到解压目录下,进入 D:/python/beautifulsoup4-4.5.3/beautifulsoup4-4.5.3
python setup.py build
python setup.py install
进行安装,一开始命令首部没有输入python,结果都不成功,后来尝试加上python,安装成功
导入
进入python,然后输入
from bs4 import BeautifulSoup
结果又报错:
Traceback (most recent call last):
File “”, line 1, in
File “D:\python\beautifulsoup4-4.5.3\beautifulsoup4-4.5.3\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
通过错误提示发现是版本不对应造成的,因此退出python,在cmd中输入
2to3 -w bs4
再次尝试导入,成功!
Tips
python3不再有urllib2,取而代之的是urllib.request,因此把在Python2中使用urllib2的地方全部替代为urllib.request即可
from BeautifulSoup import BeautifulSoup 总是会出错,替换为from bs4 import BeautifulSoup即可