Reputation: 1101
I have installed BeautifulSoup using easy_install
and I'm trying 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 get this error:
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
Upvotes: 102
Views: 149627
Reputation: 19
Don't forget to check this - It could be due to multiple installed Python versions on your system, and the library is getting installed for different version, hence not accessible for the particular version that you are trying to use. I spent a lot of time stucking into this issue but none of the provided solutions worked.
Upvotes: 0
Reputation: 21
First install beautiful soup version 4. write command in the terminal window:
pip install beautifulsoup4
then import the BeutifulSoup library
Upvotes: 2
Reputation: 1
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"))
Upvotes: 0
Reputation: 49
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
Upvotes: 0
Reputation: 480
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)
Upvotes: 6
Reputation: 3341
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
Upvotes: 0
Reputation: 141
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
Upvotes: 11
Reputation: 9585
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
Upvotes: 22
Reputation: 2764
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.
Upvotes: 263