python-ImportError:无法导入名称’BeautifulSoup4′

我知道这个问题已经在这个网站上问了很多,但是我已经尝试了所有给出的解决方案,但是我不知道如何解决这个问题.

对于初学者:我在使用Python 3.6的Windows 10计算机上.我将Anaconda安装为我的IDE.

我尝试通过pip install beautifulsoup4安装BeautifulSoup4,但是我得到了

Requirement already satisfied

响应.

我试图运行的代码仅仅是

from bs4 import BeautifulSoup4

to which I get the error: ImportError: cannot import name 'BeautifulSoup4' 

The full error is:
runfile('C:/Users/ter/.spyder-py3/untitled1.py', wdir='C:/Users/ter/.spyder-py3')
Traceback (most recent call last):

  File "<ipython-input-21-8717178e85e1>", line 1, in <module>
    runfile('C:/Users/ter/.spyder-py3/untitled1.py', wdir='C:/Users/ter/.spyder-py3')

  File "C:\Users\ter\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\ter\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/ter/.spyder-py3/untitled1.py", line 8, in <module>
    from bs4 import BeautifulSoup4

ImportError: cannot import name 'BeautifulSoup4' 

这是我尝试解决此问题的方法:

1.因为我发现的解决方案的90%是因为有人将文件命名为bs4.py,所以我检查了一下.但这是我在这台计算机上制作的第一个文件,名为“ untitled1.py”.后来(我做的最后一件事)出于沮丧,我从计算机上删除了每个bs4漂亮的实例,问题仍然存在,因此另一个名为`bs4`的文件绝对不是问题.

>我尝试仅导入bs4(导入bs4).那工作得很好,或者至少不会导致任何错误.
>我改变了周围的目录. bs4文件当前位于默认的Anaconda文件夹中.然后,我将CD更改为项目文件的位置,甚至尝试将bs4,bs4-0.0.1.dist-info和beautifulsoup4-4.6.3.dist-info文件复制并粘贴到项目文件目录中.那里什么都没有真正改变.
>我也做了pip3安装bs4.我不知道这是否必要,但我也做到了.
>我多次重新安装了所有这些软件,包括一次Anaconda和bs4 / beautifulsoup 7或8次,包括对pip install –upgrade -force-reinstall beautifulsoup4的一些使用.

无论如何,我希望这有助于描述问题.让我知道是否可以提供更多信息.

预先感谢你们给我的任何帮助!

解决方法:

正如@Blender提到的只是BeautifulSoup:

from bs4 import BeautifulSoup

html = '''<a href="some_url">next</a>
<span class="class"><a href="another_url">later</a></span>'''

soup = BeautifulSoup(html)

for a in soup.find_all('a', href=True):
    print("Found the URL:", a['href'])

输出量

Found the URL: some_url
Found the URL: another_url

上面的代码以这个question为例

上一篇:《算法设计手册》面试题解答 第四章:排序和搜索


下一篇:Cookiecutter通过项目模板创建项目