Terry Townsend
Terry Townsend

Reputation: 67

ImportError: cannot import name 'BeautifulSoup4'

I know this question has been asked a lot on this site, but I have tried all of the solutions given, and I cannot figure out how to solve this problem.

For starters: I am on a Windows 10 computer using Python 3.6. I installed Anaconda as my IDE.

I tried to install BeautifulSoup4 with pip install beautifulsoup4, but I got the

Requirement already satisfied

response.

The code I am trying to run is just

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' 

Here is what I have tried to resolve this problem:

    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. I tried just importing bs4 (import bs4). That works perfectly fine, or at least it does not lead to any errors.
  2. I changed the directories around. The bs4 file is currently in the default Anaconda folder. I then changed the CD to the location of the project file and even tried copying and pasting the bs4, bs4-0.0.1.dist-info, and beautifulsoup4-4.6.3.dist-info files into the project file directory. Nothing really changed there.
  3. I also did pip3 install bs4. I don't know if that's necessary, but I did that as well.
  4. I reinstalled all of these multiple times including Anaconda once and bs4/beautifulsoup 7 or 8 times, including a few uses of pip install --upgrade -force-reinstall beautifulsoup4.

Anyways, I hope this helps describe the problem. Let me know if I can provide any further information.

Thanks in advance for any help you guys can give me!

Upvotes: 3

Views: 5194

Answers (1)

Dani Mesejo
Dani Mesejo

Reputation: 61900

As @Blender mentioned is just 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'])

Output

Found the URL: some_url
Found the URL: another_url

The above code was taken as example from this question

Upvotes: 5

Related Questions