-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathw3lib_test.py
51 lines (42 loc) · 1.68 KB
/
w3lib_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# -*- coding: utf-8 -*-
import w3lib.html
html_str = ''' s<!--页眉(840*60) end-->
<h1 style="display:block; position:relative; clear:both">新闻标题</h1>
<div id="BaiduSpider" style="display:none">
'''
html_str = w3lib.html.remove_comments(html_str) # 去除注释
# html_str = w3lib.html.remove_tags(html_str, which_ones=('style', )) # 去除 style 标签
html_str = w3lib.html.remove_tags_with_content(html_str, which_ones=('style',)) # 去除 style 标签及其内容
print(html_str)
'''
s
<h1 style="display:block; position:relative; clear:both">新闻标题</h1>
<div id="BaiduSpider" style="display:none">
'''
print('=' * 64)
#
html_str = '你好\x20啊\xa0啊\t啊\n啊'
html_str = w3lib.html.replace_escape_chars(html_str, which_ones=('\n', '\t', '\r', '\x20', '\xa0'), replace_by='/') # 替换
print(html_str)
'''
你好/啊/啊/啊/啊
'''
print('=' * 64)
#
html_str = '''你好\x20啊\xa0啊\t啊\n啊
<p class="otitle"> 测试标题测试标题测试标题测试标题</p>
<p> </p>
<p> “测试内容测试内容测试内容测试内容”——测试内容测试内容</p>
<p> 测试内容测试内容“测试内容测试内容”测试内容</p>
'''
html_str = w3lib.html.remove_tags(html_str) # 去除html标签
html_str = w3lib.html.replace_entities(html_str) # 替换html实体
print(html_str)
'''你好 啊 啊 啊
啊
测试标题测试标题测试标题测试标题
“测试内容测试内容测试内容测试内容”——测试内容测试内容
测试内容测试内容“测试内容测试内容”测试内容
'''
print('=' * 64)