获取html编码
from w3lib import encoding
str_html = '''
<!--meta-->
<meta charset=utf-8>
£
<!--这是注释-->
'''
print(html.remove_comments(str_html))
head编码
from w3lib import encoding
print(encoding.http_content_type_encoding("Content-Type: text/html; charset=ISO-8859-4"))
Bom
from w3lib import encoding
print(encoding.read_bom(b'\xfe\xff\x6c\x34'))
去除Html标签(remove_tags)
from w3lib import html
'''remove_tags(text, which_ones=(), keep=(), encoding=None)'''
- 保留p标签
res_html = html.remove_tags(str_html, keep=('p',))
- 选择去除的标签:
res_html = html.remove_tags(str_html, which_ones=('p',))
- which_ones 、keep 不能同时使用
去除标签和内容(remove_tags_with_content)
res_html = html.remove_tags_with_content(str_html, which_ones=('p',))
html转实体(replace_entities)
str_html = '''
£
'''
res_html = html.replace_entities(str_html, keep=('£'))
去除Html标签并替换(replace_tags)
str_html = '''£ This text contains <a>some tag</a>'''
res_html = html.replace_tags(str_html, '--')
过滤注释(remove_comments)
''' remove_comments(text, encoding=None) '''
from w3lib import html
str_html = '''
£
<!--这是注释-->
'''
print(html.remove_comments(str_html))