Python 我如何从 bs4 导入 BeautifulSoup?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18831380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:59:04  来源:igfitidea点击:

How can I from bs4 import BeautifulSoup?

pythonbeautifulsoup

提问by Mohammad

This code:

这段代码:

from bs4 import BeautifulSoup  

Doesn't work, and gives this error :

不起作用,并出现此错误:

raise AttributeError, "'%s' object has no attribute '%s'" % (self.__class__.__name__,attr)    
                    ^
SyntaxError: invalid syntax    

What should i do ?

我该怎么办 ?

采纳答案by Jordan

You should be using pipto install, so you can simply do

你应该pip用来安装,所以你可以简单地做

pip install beautifulsoup4

That will install the latest BS4, which is 4.3.1 as of 2013-08-15. It supports Python 3.

这将安装最新的 BS4,即 2013-08-15 的 4.3.1。它支持 Python 3。

回答by Goulouh Anwar

For Windows... Go to start menu type cmd right click on cmd icon click run as administrator then type pip install beautifulsoup4.

对于 Windows... 转到开始菜单键入 cmd 右键单击​​ cmd 图标单击以管理员身份运行,然后键入 pip install beautifulsoup4。

It likely will fail to install correctly if you fail to do the above step as even though your windows user is an admin account it does not run all apps as administrator.

如果您未能执行上述步骤,它可能无法正确安装,因为即使您的 Windows 用户是管理员帐户,它也不会以管理员身份运行所有应用程序。

Notice the difference if you simply just open cmd without the run as admin.

如果您只是打开 cmd 而没有以管理员身份运行,请注意区别。

Remember also when using it like so...

还记得像这样使用它时......

from bs4 import beautifulsoup4

Will not work as it is not correctly formatted.

将无法工作,因为它的格式不正确。

from bs4 import BeautifulSoup4

Will work correctly as it is CaseSensitive.

将正常工作,因为它是 CaseSensitive。

回答by tonhozi

Also, if you are using python3, you should use:

另外,如果您使用的是 python3,则应该使用:

pip3 install beautifulsoup4