我已经从 https://www.crummy.com/software/BeautifulSoup/bs4/download/4.5/下载了beautifulsoup4-4.5.3.tar.gz并将其解压缩到我的python工作目录(这不是我的python安装目录)。
但是,当我跑步时
from bs4 import BeautifulSoup
在我的空闲中弹出错误按摩:
>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
from bs4 import BeautifulSoup
File "D:pythonbeautifulsoup4-4.5.3beautifulsoup4-4.5.3bs4__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
我尝试了这些方法,但上面的错误按摩不断弹出
- 在我的 IDLE 中打开 setup.py 并运行它(在空闲窗口中提供
=== RESTART: D:pythonbeautifulsoup4-4.5.3beautifulsoup4-4.5.3setup.py ===
,但from bs4 import BeautifulSoup
不起作用)
使用 cmd 并转到 D:\python\beautifulsoup4-4.5.3 - \beautifulsoup4-4.5.3,运行
pip uninstall beautifulsoup4
然后运行pip install beautifulsoup4
;它表明我已经在cmd行中成功安装了beautifulsoup4-4.5.3,但是,from bs4 import BeautifulSoup
后仍然在空闲中出现错误按摩 - 使用 CMD 并转到 D:\python\beautifulsoup4-4.5.3\beautifulsoup4-4.5.3,运行
pip3 uninstall beautifulsoup4
然后运行pip3 install beautifulsoup4
; 如上无用 - 运行
pip install bs4 --ignore-installed
,如上无用 - 运行
setup.py install
,如上无用 - 在 D:\python\beautifulsoup4-4.5.3\beautifulsoup4-4.5.3 下的 cmd 行中运行
2to3 -w bs4
,返回'2to3' is not recognized as an internal or external command,operable program or batch file.
我该怎么办?
除此之外,pip show bs4
给出了这个
`Metadata-Version: 1.1
Name: bs4
Version: 0.0.1
Summary: Screen-scraping library
Home-page: https://pypi.python.org/pypi/beautifulsoup4
Author: Leonard Richardson
Author-email: leonardr@segfault.org
License: MIT
Location: c:usersmynameappdatalocalprogramspythonpython35-
32libsite-packages
Requires: beautifulsoup4
You are using pip version 7.1.2, however version 9.0.1 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip'
command.`
在我的 C:\Users\myname\AppData\Local\Programs\Python\Python35-32\Lib\site-packages 目录下,我可以看到三个与 beautifulsoup 相关的目录:beautifulsoup4-4.5.3.dist-info、bs4 和 bs4-0.0.1-py3.5.egg-info,但from bs4 import BeautifulSoup
不断抛出错误的信息
BeautifulSoup4 与 Python 3x 和 2x 兼容,但安装过程略有不同。以下是我如何为 Python 3x 安装它。
转到 http://www.crummy.com/software/BeautifulSoup/#Download 或 https://pypi.python.org/pypi/beautifulsoup4/4.3.2 并下载美丽汤4-4.3.2.tar.gz。
导航到下载文件的文件夹。使用 7zip 提取 beautifulsoup4-4.3.2.tar.gz 文件。
双击 beautifulsoup4-4.3.2.tar.gz 文件夹以向下钻取 dist 文件夹,然后解压缩 beautifulsoup4-4.3.2.tar 文件。
双击 beautifulsoup4-4.3.2.tar 文件夹向下钻取另一个 beautifulsoup4-4.3.2 文件夹。在 beautifulsoup4-4.3.2 文件夹中,您将看到 setup.py 文件。
按住 Shift 键,同时右键单击文件夹内的任意位置,然后选择在此处打开命令窗口。
在命令提示符下键入:setup.py 安装并按 Enter 键。
当安装停止运行时,通过导航到 C:\Python34\Lib\site-packages 目录来验证是否已安装包。如果已安装,您将看到 bs4 目录。
通过转到 Python 3.4>IDLE(Python 3.4 GUI 32 位)>所有程序的开始>启动来验证安装是否正常工作。
提示后键入:>>>从 bs4 导入美丽汤
如果您没有收到任何错误,则它正在工作。
这个问题分三步解决:
首先,删除python安装目录下所有与beautifulsoup相关的目录和文件,我的意思是这个C:\Users\yourname\AppData\Local\Programs\Python\Python35-32\Lib\site-packages下的所有beautifulsoup相关目录和文件
其次,运行pip uninstall beautifulsoup4
然后在 cmd 行中的 D:\python\beautifulsoup4-4.5.3\beautifulsoup4-4.5.3 下pip install beautifulsoup4
,即 setup.py 所在的地方。
第三,您需要在 IDLE 和运行模块中打开 setup.py
最后,在主 IDLE 窗口中运行from bs4 import BeautifulSoup
。现在没有错误消息。