ImportError:无法导入名称“BeautifulSoup4”

2024-04-25 21:42:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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

首先:我在Windows 10计算机上使用Python 3.6。我安装了Anaconda作为我的IDE。在

我试图用pip安装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”,所以我进行了检查。不过,这是我在这台计算机上制作的第一个文件,名为“untitle1.py”。后来(我做的最后一件事)出于沮丧,我从我的计算机上删除了bs4和Beauty的每个实例,问题仍然存在,因此另一个名为“bs4”的文件绝对不是问题。
  1. 我尝试导入bs4(importbs4)。这样做非常好,或者至少不会导致任何错误。在
  2. 我把目录改了。bs4文件当前位于默认的Anaconda文件夹中。然后我将CD更改为项目文件的位置,甚至尝试将bs4bs4-0.0.1.dist-info和{}文件复制并粘贴到项目文件目录中。没有什么真正的改变。在
  3. 我还安装了pip3安装bs4。我不知道有没有必要,但我也这么做了。在
  4. 我多次重新安装了这些文件,包括Anaconda一次和{}7次或8次,包括一些{}的用法。在

无论如何,我希望这有助于描述问题。如果我能提供更多的信息,请告诉我。在

提前谢谢你们能给我的任何帮助!在


Tags: 文件inpyimport计算机linepy3site
1条回答
网友
1楼 · 发布于 2024-04-25 21:42:49

正如@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'])

输出

^{pr2}$

上面的代码是从这个question中获取的示例

相关问题 更多 >