ImportError: No module named BeautifulSoup

PythonBeautifulsoup

Python Problem Overview


I have installed BeautifulSoup using easy_install and trying to run following script

from BeautifulSoup import BeautifulSoup
import re

doc = ['<html><head><title>Page title</title></head>',
       '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
       '<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
       '</html>']
soup = BeautifulSoup(''.join(doc))

print soup.prettify()

But not sure why this is happening

Traceback (most recent call last):
  File "C:\Python27\reading and writing xml file from web1.py", line 49, in <module>
    from BeautifulSoup import BeautifulSoup
ImportError: No module named BeautifulSoup

Could you please help. Thanks

Python Solutions


Solution 1 - Python

Try this from bs4 import BeautifulSoup

This might be a problem with Beautiful Soup, version 4, and the beta days. I just read this from the homepage.

Solution 2 - Python

On Ubuntu 14.04 I installed it from apt-get and it worked fine:

sudo apt-get install python-beautifulsoup

Then just do:

from BeautifulSoup import BeautifulSoup

Solution 3 - Python

Try This, Mine worked this way. To get any data of tag just replace the "a" with the tag you want.

from bs4 import BeautifulSoup as bs
import urllib

url="http://currentaffairs.gktoday.in/month/current-affairs-january-2015"

soup = bs(urllib.urlopen(url))
for link in soup.findAll('a'):
        print link.string

Solution 4 - Python

you can import bs4 instead of BeautifulSoup. Since bs4 is a built-in module, no additional installation is required.

from bs4 import BeautifulSoup
import re

doc = ['<html><head><title>Page title</title></head>',
       '<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
       '<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
       '</html>']
soup = BeautifulSoup(''.join(doc))

print soup.prettify()

If you want to request, using requests module. request is using urllib, requests modules. but I personally recommendation using requests module instead of urllib

module install for using:

$ pip install requests

Here's how to use the requests module:

import requests as rq
res = rq.get('http://www.example.com')

print(res.content)
print(res.status_code)

Solution 5 - Python

First install beautiful soup version 4. write command in the terminal window:

pip install beautifulsoup4

then import the BeutifulSoup library

Solution 6 - Python

if you got two version of python, maybe my situation could help you

this is my situation

1-> mac osx

2-> i have two version python , (1) system default version 2.7 (2) manually installed version 3.6

3-> i have install the beautifulsoup4 with sudo pip install beautifulsoup4

4-> i run the python file with python3 /XXX/XX/XX.py

so this situation 3 and 4 are the key part, i have install beautifulsoup4 with "pip" but this module was installed for python verison 2.7, and i run the python file with "python3". so you should install beautifulsoup4 for the python 3.6;

with the sudo pip3 install beautifulsoup4 you can install the module for the python 3.6

Solution 7 - Python

I had the same problem with eclipse on windows 10.

I installed it like recommende over the windows command window (cmd) with:

C:\Users\NAMEOFUSER\AppData\Local\Programs\Python\beautifulsoup4-4.8.2\setup.py install 

BeautifulSoup was install like this in my python directory:

C:\Users\NAMEOFUSE\AppData\Local\Programs\Python\Python38\Lib\site-packages\beautifulsoup4-4.8.2-py3.8.egg

After manually coping the bs4 and EGG-INFO folders into the site-packages folder everything started to work, also the example:

from bs4 import BeautifulSoup


html = """
    <html>
        <body>
            <p> Ich bin ein Absatz!</p>
        </body>
    </html>
"""
print(html)


soup = BeautifulSoup(html, 'html.parser')

print(soup.find_all("p"))

Solution 8 - Python

if you installed its this way(if you not, installing this way):

pip install beautifulsoup4

and if you used its this code(if you not, use this code):

from bs4 import BeautifulSoup

if you using windows system, check it if there are module, might saved different path its module

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMuhammad ImranView Question on Stackoverflow
Solution 1 - PythonJames R. ClaytonView Answer on Stackoverflow
Solution 2 - PythonCaumonsView Answer on Stackoverflow
Solution 3 - Pythonicodekamal.comView Answer on Stackoverflow
Solution 4 - Python멍개-mungView Answer on Stackoverflow
Solution 5 - PythonNikruti MojidraView Answer on Stackoverflow
Solution 6 - PythonJeffery MaView Answer on Stackoverflow
Solution 7 - PythonCoronaChaosView Answer on Stackoverflow
Solution 8 - PythonRecep BüyükfıratView Answer on Stackoverflow