使用BeautifulSoup从网页中提取文本

时间:2017-02-21 22:41:38

标签: python beautifulsoup

我是Python编程的新手,我刚刚安装了Python(版本3.5.2),我使用Geany编辑器来编写和执行我的脚本。

我刚试过这个简单的脚本,但它失败了:

from bs4 import BeautifulSoup
import urllib
r = urllib.urlopen('http://www.aflcio.org/Legislation-and-Politics/Legislative-Alerts').read()
soup = BeautifulSoup(r)

错误:

Traceback (most recent call last):
  File "soup.py", line 1, in <module>
    from bs4 import BeautifulSoup
ImportError: No module named 'bs4'

我已使用pip install bs4安装了BeautifulSoup,我获得了成功输出“Successfully installed bs4-0.0.1”

我知道这是一个简单的问题,但任何帮助都将不胜感激!

2 个答案:

答案 0 :(得分:2)

错误意味着您的口译员找不到bs4。如果您运行pip install bs4,请尝试以下操作:

pip3 install bs4

pip3是Python3特定的点子。这个答案不适用于所有平台,但你没有提供你的答案:)

答案 1 :(得分:1)

在命令行/终端输入“pip freeze”。如果在该列表中没有看到bs4-0.0.1,则表示未安装。

如果你看到它,你可以尝试“pip uninstall bs4”然后重新安装“pip install bs4”。

另外,请记住“pip install”是命令行/终端命令。您不要在Python解释器中键入它:在命令行中键入它

相关问题