-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_analyze.py
More file actions
135 lines (100 loc) · 3.25 KB
/
Copy pathtest_analyze.py
File metadata and controls
135 lines (100 loc) · 3.25 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import gumbocy
from test_word_groups import TAGS_SEPARATORS
def analyze(html, options=None):
parser = gumbocy.HTMLParser(options=options)
parser.parse(html)
return parser.analyze()
def test_separators():
html = """
<p>text</p>
<p>text 2</p>
<p>pre<p>inner</p></p>
"""
analyzed = analyze(html, options={
"tags_separators": ["p"]
})
assert analyzed["word_groups"] == [
("text", "p"),
("text 2", "p"),
("pre", "p"),
("inner", "p")
]
# More word group tests in test_word_groups.py
def test_hidden_text():
html = """<html><head></head><body>
<!-- comment -->
text
<div>textp</div>
<div style='display: none;'>hidden by display</div>
<div class='_class_noindex'>ignored by class_noindex</div>
<div class='_class_noindex class2'>ignored by class_noindex 2</div>
<div hidden>hidden by html5 attribute</div>
<div aria-hidden="true">hidden by aria</div>
<div aria-hidden="false">not_aria</div>
<div style='visibility: hidden;'>hidden by visibility</div>
</body></html>"""
analyzed = analyze(html, options={
"attributes_whitelist": ["style", "hidden", "aria-hidden"],
"classes_hidden": ["_class_hidden"],
"ids_hidden": ["_id_hidden"],
"tags_separators": ["div"],
"classes_ignore": ["_class_noindex"]
})
assert analyzed["word_groups"] == [
("text", "body"),
("textp", "div"),
("not_aria", "div")
]
def test_hidden_siblings():
html = """
<span class='login facebook'>
Sign in with Facebook
</span>
<span class='login'>Or use your Businessweek account</span>
"""
analyzed = analyze(html, options={
"classes_boilerplate": ["login"]
})
assert analyzed["word_groups"] == []
def test_boilerplate_text():
html = """<html><head></head><body>
<header>
Boilerplate
<h2>Title</h2>
</header>
<div class="classboil">x</div>
<div id="idboil">y</div>
<div role="roleboil">z</div>
<h2>Title 2</h2>
</body></html>"""
analyzed = analyze(html, options={
"attributes_whitelist": ["id", "class", "role"],
"tags_boilerplate": ["header"],
"tags_boilerplate_bypass": ["h2"],
"classes_boilerplate": ["classboil"],
"ids_boilerplate": ["idboil"],
"roles_boilerplate": ["roleboil"],
"tags_separators": TAGS_SEPARATORS
})
assert analyzed["word_groups"] == [
("Title", "h2"),
("Title 2", "h2")
]
def test_title():
html = """ <title>test 1</title> <title>test 2</title> """
analyzed = analyze(html, options={
})
assert analyzed["title"] == "test 1"
assert len(analyzed["word_groups"]) == 0
def test_head_metas():
html = """<html>
<head>
<meta name="Description" content=" This is a <summary>!" />
<meta name="Description2" content=" This2 is a <summary>!" />
</head>
<body>This is <body> text</body>
</html>"""
analyzed = analyze(html, options={
"metas_whitelist": ["description"]
})
assert analyzed["head_metas"] == {"description": "This is a <summary>!"}