In python3 import bs4 not BeautifulSoup which will work only in python2

This commit is contained in:
Kudlaty
2013-04-16 10:50:07 +02:00
parent 44647b72f1
commit 20e99f3882

View File

@@ -4,6 +4,7 @@ from django.core.exceptions import ImproperlyConfigured
from compressor.exceptions import ParserError from compressor.exceptions import ParserError
from compressor.parser import ParserBase from compressor.parser import ParserBase
from compressor.utils.decorators import cached_property from compressor.utils.decorators import cached_property
from django.utils import six
try: try:
from django.utils.encoding import smart_text from django.utils.encoding import smart_text
@@ -17,6 +18,9 @@ class BeautifulSoupParser(ParserBase):
@cached_property @cached_property
def soup(self): def soup(self):
try: try:
if six.PY3:
from bs4 import BeautifulSoup
else:
from BeautifulSoup import BeautifulSoup from BeautifulSoup import BeautifulSoup
return BeautifulSoup(self.content) return BeautifulSoup(self.content)
except ImportError as err: except ImportError as err:
@@ -25,9 +29,15 @@ class BeautifulSoupParser(ParserBase):
raise ParserError("Error while initializing Parser: %s" % err) raise ParserError("Error while initializing Parser: %s" % err)
def css_elems(self): def css_elems(self):
if six.PY3:
return self.soup.find_all({'link': True, 'style': True})
else:
return self.soup.findAll({'link': True, 'style': True}) return self.soup.findAll({'link': True, 'style': True})
def js_elems(self): def js_elems(self):
if six.PY3:
return self.soup.find_all('script')
else:
return self.soup.findAll('script') return self.soup.findAll('script')
def elem_attribs(self, elem): def elem_attribs(self, elem):