ImportError: no module named BeautifulSoup

I installed BeautifulSoup using easy_install and tried to run the 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 I don’t know 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 help me. Thanks

+54
python beautifulsoup
Apr 14 2018-11-11T00:
source share
5 answers

Try from bs4 import BeautifulSoup

This could be a problem with Beautiful Soup, version 4 and beta versions. I just read it from the home page.

+167
Jun 11 2018-12-12T00:
source share

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

sudo apt-get install python-beautifulsoup

Then just do:

from BeautifulSoup import BeautifulSoup

+18
Oct 09 '14 at 2:57
source share

Try it, mine worked like that. To get any tag data, simply 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 
+8
Jan 27 '16 at 8:37
source share

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 query using the query module. The request uses urllib , requests modules. but I personally recommend using the requests module instead of urllib

install the module to use:

 $ pip install requests 

Here's how to use the query module:

 import requests as rq res = rq.get('http://www.example.com') print(res.content) print(res.status_code) 
+1
06 Oct '17 at 4:05
source share

if you have two versions of python, maybe my situation can help you

this is my situation.

1-> mac osx

2-> I have two python options, (1) the default system version is 2.7 (2) manually installed version 3.6

3-> I installed beautifulsoup4 with sudo pip install beautifulsoup4

4-> Run python file with python3 /XXX/XX/XX.py

so this situation 3 and 4 are the key part, I have beautifulsoup4 with "pip", but this module was installed for python verison 2.7 and I am running the python file with "python3". therefore you must install beautifulsoup4 for python 3.6;

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

0
May 16 '17 at 10:47
source share



All Articles