爬虫好搭档之 w3lib

获取html编码
from w3lib import encoding
str_html = '''
<!--meta-->
<meta charset=utf-8>
&pound;
<!--这是注释-->
'''
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)'''
  1. 保留p标签
res_html = html.remove_tags(str_html, keep=('p',))
  1. 选择去除的标签:
res_html = html.remove_tags(str_html, which_ones=('p',))
  1. which_ones 、keep 不能同时使用
去除标签和内容(remove_tags_with_content)
res_html = html.remove_tags_with_content(str_html, which_ones=('p',))
html转实体(replace_entities)
str_html = '''
    &pound;
    &nbsp;
 '''
res_html = html.replace_entities(str_html, keep=('&pound;'))

去除Html标签并替换(replace_tags)
    str_html = '''&pound;&nbsp;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 = '''
&pound;
<!--这是注释-->
'''
print(html.remove_comments(str_html))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值