繁体   English   中英

我尝试在 mac os 终端中运行我的 python 程序,但在我成功安装 Beautifulsoup4 后不断收到以下反馈

[英]I tried running my python program in a mac os terminal but keep getting the feedback below after I have successfully installed Beautifulsoup4

import urllib
from Beautifulsoup import*

url = raw_input('Enter - ')

html = urllib.urlopen(url).read()
soup = Beautifulsoup(html)

#Retrieve a list of the anchor tags
#Each tag is like a dictionary of HTML attributes

tags = soup('a')

for tag in tags:
    print tag.get('href', None)

这是我试图运行的代码,下面是我从终端得到的响应:

MacBook-Pro-3:documents frankline$ python ./parsing_html.py
Traceback (most recent call last):
  File "./parsing_html.py", line 2, in <module>
    from Beautifulsoup import*
ImportError: No module named Beautifulsoup

但是,我已经安装了 Beautifulsoup,如下所示:

MacBook-Pro-3:documents frankline$ sudo pip install Beautifulsoup4
The directory '/Users/frankline/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/frankline/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting Beautifulsoup4
  Downloading beautifulsoup4-4.5.3-py2-none-any.whl (85kB)
    100% |████████████████████████████████| 92kB 151kB/s 
Installing collected packages: Beautifulsoup4
Successfully installed Beautifulsoup4-4.5.3

请帮我检查错误

您的导入错误, from Beautifulsoup import*更改为:

from bs4 import BeautifulSoup

您可能正在寻找 Beautiful Soup 3 的文档。如果是这样,您应该知道 Beautiful Soup 3 已不再开发,并且建议所有新项目都使用 Beautiful Soup 4。 如果您想了解 Beautiful Soup 3 和 Beautiful Soup 4 之间的区别,请参阅Porting code to BS4

在这里阅读 bs4 库

请阅读文件

from bs4 import BeautifulSoup

soup = BeautifulSoup(open("index.html"))

soup = BeautifulSoup("<html>data</html>")

每当遇到ImportError 时,请先尝试在 python 控制台中导入。

请使用此导入语句来使用 BeautifulSoup4。

from bs4 import BeautifulSoup

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM