5

I am trying to use beautifulsoup compatible lxml and it is giving me an error:

from lxml.html.soupparser import fromstring
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Python/2.7/site-packages/lxml/html/soupparser.py", line 7, in <module>
    from BeautifulSoup import \
ImportError: No module named BeautifulSoup

I have bs4 installed. How do I fix this issue?

Vaibhav Mule
  • 5,016
  • 4
  • 35
  • 52
raju
  • 4,788
  • 15
  • 64
  • 119

3 Answers3

10

The error is caused by soupparser.py trying to import BeautifulSoup version 3 while you have version 4 installed. The module name was changed from BeautifulSoup to bs4 in version 4.

You can trick soupparser.py into importing version 4 by mapping the bs4 module to BeautifulSoup in sys.modules before importing soupparser:

import sys, bs4
sys.modules['BeautifulSoup'] = bs4

from lxml.html.soupparser import fromstring
Anonymous Coward
  • 6,186
  • 1
  • 23
  • 29
  • 4
    Does soupparser work fine with bs4? If so, why does it still use an old version? Should we go and change soupparser to try importing bs4 as BeautifulSoup, and then only import BeautifulSoup if that fails? Assuming soupparser.py is available for modification in lxml's Github repo, making the change and eliminating this problem for future users should be trivial, right? – Mark Amery Mar 03 '13 at 15:51
  • Soupparser is now ported to bs4. See separate answer. – Emil Stenström Sep 02 '15 at 12:00
0

There is now a version of soupparser that works with bs4. It's available here: https://github.com/lxml/lxml/blob/master/src/lxml/html/soupparser.py

Emil Stenström
  • 13,329
  • 8
  • 53
  • 75
-1

Try adding :

from bs4 import BeautifulSoup

and make sure you have the correct version of BeautifulSoup for your system installed.

Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
Aleksander S
  • 368
  • 1
  • 10
  • 1
    I have beautifulsoup version 4 installed so from bs4 import BeautifulSoup works well but lxml.html.soupparser uses beautifulsoup 3 and has statement from BeautifulSoup import xyz. – raju Dec 27 '12 at 09:36