我知道这个问题已经在这个网站上被问了很多,但我已经尝试了所有给出的解决方案,我无法弄清楚如何解决这个问题。
对于初学者:我使用Windows 10
在Python 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'
以下是我试图解决此问题的方法:
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为例