BeautifulSoup安装以及一些错误

来源:互联网 发布:社交网络的好处知乎 编辑:程序博客网 时间:2024/06/04 18:38

    • 下载
    • 安装
    • 导入
    • Tips


下载

由于需要用到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

  1. python3不再有urllib2,取而代之的是urllib.request,因此把在Python2中使用urllib2的地方全部替代为urllib.request即可

  2. from BeautifulSoup import BeautifulSoup 总是会出错,替换为from bs4 import BeautifulSoup即可

0 0
原创粉丝点击