From 20e99f3882b01b320e383f2784968bd059ec5844 Mon Sep 17 00:00:00 2001 From: Kudlaty Date: Tue, 16 Apr 2013 10:50:07 +0200 Subject: [PATCH] In python3 import bs4 not BeautifulSoup which will work only in python2 --- compressor/parser/beautifulsoup.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/compressor/parser/beautifulsoup.py b/compressor/parser/beautifulsoup.py index 8218768..bddd39a 100644 --- a/compressor/parser/beautifulsoup.py +++ b/compressor/parser/beautifulsoup.py @@ -4,6 +4,7 @@ from django.core.exceptions import ImproperlyConfigured from compressor.exceptions import ParserError from compressor.parser import ParserBase from compressor.utils.decorators import cached_property +from django.utils import six try: from django.utils.encoding import smart_text @@ -17,7 +18,10 @@ class BeautifulSoupParser(ParserBase): @cached_property def soup(self): try: - from BeautifulSoup import BeautifulSoup + if six.PY3: + from bs4 import BeautifulSoup + else: + from BeautifulSoup import BeautifulSoup return BeautifulSoup(self.content) except ImportError as err: raise ImproperlyConfigured("Error while importing BeautifulSoup: %s" % err) @@ -25,10 +29,16 @@ class BeautifulSoupParser(ParserBase): raise ParserError("Error while initializing Parser: %s" % err) def css_elems(self): - return self.soup.findAll({'link': True, 'style': True}) + if six.PY3: + return self.soup.find_all({'link': True, 'style': True}) + else: + return self.soup.findAll({'link': True, 'style': True}) def js_elems(self): - return self.soup.findAll('script') + if six.PY3: + return self.soup.find_all('script') + else: + return self.soup.findAll('script') def elem_attribs(self, elem): return dict(elem.attrs)