ImportError:无法导入名称'BeautifulSoup4'

问题描述 投票:3回答:1

我知道这个问题已经在这个网站上被问了很多,但我已经尝试了所有给出的解决方案,我无法弄清楚如何解决这个问题。

对于初学者:我使用Windows 10Python 3.6计算机上。我安装了Anaconda作为我的IDE。

我试图用BeautifulSoup4安装pip安装beautifulsoup4,但我得到了

要求已经满足

响应。

我试图运行的代码就是

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. Since 90% of the solutions I have found were because someone had named a file `bs4.py`, I checked for that. However this was the very first file I had made on this computer which was just named `untitled1.py`. Later on (last thing I did) out of frustration I deleted every instance of bs4 and beautiful from my computer, and the problem persisted, so another file being named `bs4` is definitely not the issue.
  1. 我尝试过导入bs4(导入bs4)。这完全正常,或者至少它不会导致任何错误。
  2. 我改变了目录。 bs4文件当前位于默认的Anaconda文件夹中。然后我将CD更改为项目文件的位置,甚至尝试将bs4bs4-0.0.1.dist-infobeautifulsoup4-4.6.3.dist-info文件复制并粘贴到项目文件目录中。那里没有什么改变。
  3. 我也做了pip3安装bs4。我不知道这是否必要,但我也是这样做的。
  4. 我重新安装了所有这些,包括Anaconda一次和bs4/beautifulsoup 7或8次,包括pip install --upgrade -force-reinstall beautifulsoup4的一些用途。

无论如何,我希望这有助于描述问题。如果我能提供任何进一步的信息,请告诉我。

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

python python-3.x beautifulsoup anaconda
1个回答
3
投票

正如@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为例

© www.soinside.com 2019 - 2024. All rights reserved.