readabilite: avoid double parsing of html
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
87d2fe772d
commit
afc31eb6e9
|
@ -19,19 +19,12 @@ import re
|
||||||
|
|
||||||
import lxml.etree
|
import lxml.etree
|
||||||
import lxml.html
|
import lxml.html
|
||||||
from bs4 import BeautifulSoup
|
import lxml.html.soupparser
|
||||||
|
|
||||||
|
|
||||||
def parse(data, encoding=None):
|
def parse(data, encoding=None):
|
||||||
if encoding:
|
kwargs = {'from_encoding': encoding} if encoding else {}
|
||||||
data = BeautifulSoup(data, 'lxml', from_encoding=encoding).prettify('utf-8')
|
return lxml.html.soupparser.fromstring(data, **kwargs)
|
||||||
|
|
||||||
else:
|
|
||||||
data = BeautifulSoup(data, 'lxml').prettify('utf-8')
|
|
||||||
|
|
||||||
parser = lxml.html.HTMLParser(remove_comments=True, encoding='utf-8')
|
|
||||||
|
|
||||||
return lxml.html.fromstring(data, parser=parser)
|
|
||||||
|
|
||||||
|
|
||||||
def count_words(string):
|
def count_words(string):
|
||||||
|
|
Loading…
Reference in New Issue