mirror of
https://github.com/searxng/searxng
synced 2024-01-01 18:24:07 +00:00
[enh] utils.py added
This commit is contained in:
parent
432ec664a3
commit
e946752474
26
searx/utils.py
Normal file
26
searx/utils.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
from HTMLParser import HTMLParser
|
||||||
|
import htmlentitydefs
|
||||||
|
|
||||||
|
class HTMLTextExtractor(HTMLParser):
|
||||||
|
def __init__(self):
|
||||||
|
HTMLParser.__init__(self)
|
||||||
|
self.result = [ ]
|
||||||
|
|
||||||
|
def handle_data(self, d):
|
||||||
|
self.result.append(d)
|
||||||
|
|
||||||
|
def handle_charref(self, number):
|
||||||
|
codepoint = int(number[1:], 16) if number[0] in (u'x', u'X') else int(number)
|
||||||
|
self.result.append(unichr(codepoint))
|
||||||
|
|
||||||
|
def handle_entityref(self, name):
|
||||||
|
codepoint = htmlentitydefs.name2codepoint[name]
|
||||||
|
self.result.append(unichr(codepoint))
|
||||||
|
|
||||||
|
def get_text(self):
|
||||||
|
return u''.join(self.result)
|
||||||
|
|
||||||
|
def html_to_text(html):
|
||||||
|
s = HTMLTextExtractor()
|
||||||
|
s.feed(html)
|
||||||
|
return s.get_text()
|
Loading…
Reference in New Issue
Block a user