您的位置首页百科问答

from bs4 import BeautifulSoup报错

from bs4 import BeautifulSoup报错

的有关信息介绍如下:

from bs4 import BeautifulSoup报错

Windows下安装BeautifulSoup4完后,from bs4 import BeautifulSoup报错:

>>> from bs4 import BeautifulSoup

Traceback (most recent call last):

File "", line 1, in

File "C:\Users\xxx\AppData\Local\Programs\Python\Python35\beautifulsoup4-4.5.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

解决办法:直接将压缩文件中的bs4复制到python安装目录下的lib中,然后再利用python自带工具2to3.py将版本2下的.py 文件转化为版本3下的文件。

2to3.py位置:在python安装目录下的Tools/scripts/2to3.py。

最后,将bs4文件夹和2to3.py同时放到lib中,然后在cmd中定位到lib,运行:2to3.py bs4 -w就好了。

验证是否成功,通过进入python,运行:from bs4 import BeautifulSoup

发现已正常!

2to3.py 用法:2to3.py param1 (-w)

param1 可以是要转换的.py文件,或者是文件夹,如果是文件夹,整个文件夹中的.py都会被转换。

-w可选,如果不写的话默认输出转换后的结果到显示屏,如果要把转换的文件再写入原文件,就需要加上。