mirror of https://github.com/scrapy/scrapy.git
Added new functions to parse html.
--HG-- extra : convert_revision : svn%3Ab85faa78-f9eb-468e-a121-7cced6da292c%40295
This commit is contained in:
parent
71c3cea112
commit
453714b252
|
|
@ -59,3 +59,42 @@ def replace_tags(text, token=''):
|
|||
Always returns a unicode string.
|
||||
"""
|
||||
return _tag_re.sub(token, text.decode('utf-8'))
|
||||
|
||||
|
||||
def remove_comments(text):
|
||||
""" Remove HTML Comments. """
|
||||
return re.sub('<!--.*?-->', '', text, re.DOTALL)
|
||||
|
||||
def remove_tags(text, which_ones=()):
|
||||
""" Remove HTML Tags only.
|
||||
|
||||
which_ones -- is a tuple of which tags we want to remove.
|
||||
if is empty remove all tags.
|
||||
"""
|
||||
if len(which_ones) > 0:
|
||||
tags = [ '<%s>|<%s .*?>|</%s>' % (tag,tag,tag) for tag in which_ones ]
|
||||
reg_exp_remove_tags = '|'.join(tags)
|
||||
else:
|
||||
reg_exp_remove_tags = '<.*?>'
|
||||
re_tags = re.compile(reg_exp_remove_tags, re.DOTALL)
|
||||
return re_tags.sub('', text)
|
||||
|
||||
def remove_tags_with_content(text, which_ones=()):
|
||||
""" Remove tags and its content.
|
||||
|
||||
which_ones -- is a tuple of which tags with its content we want to remove.
|
||||
if is empty do nothing.
|
||||
"""
|
||||
tags = [ '<%s.*?</%s>' % (tag,tag) for tag in which_ones ]
|
||||
re_tags_remove = re.compile('|'.join(tags), re.DOTALL)
|
||||
return re_tags_remove.sub('', text)
|
||||
|
||||
def remove_escape_chars(text, which_ones=('\n','\t','\r')):
|
||||
""" Remove escape chars. Default : \\n, \\t, \\r
|
||||
|
||||
which_ones -- is a tuple of which escape chars we want to remove.
|
||||
if is empty do nothing.
|
||||
"""
|
||||
re_escape_chars = re.compile('[%s]' % ''.join(which_ones))
|
||||
return re_escape_chars.sub('', text)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue