-
-
Notifications
You must be signed in to change notification settings - Fork 559
/
test_basic.txt
235 lines (205 loc) · 7.11 KB
/
test_basic.txt
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
lxml.html adds a find_class method to elements::
>>> from lxml.etree import Comment
>>> from lxml.html import document_fromstring, fragment_fromstring, tostring
>>> from lxml.html import fragments_fromstring, fromstring
>>> from lxml.html import usedoctest
>>> try: unicode = unicode
... except NameError: unicode = str
>>> h = document_fromstring('''
... <html><head></head>
... <body>
... <a class="vcard
... fn url" href="foobar">P1</a>
... <a class="not-fn vcard" href="baz">P2</a>
... </body></html>''')
>>> print(tostring(h, encoding=unicode))
<html>
<head></head>
<body>
<a class="vcard
fn url" href="foobar">P1</a>
<a class="not-fn vcard" href="baz">P2</a>
</body>
</html>
>>> print([e.text for e in h.find_class('fn')])
['P1']
>>> print([e.text for e in h.find_class('vcard')])
['P1', 'P2']
as well as the ability to toggle classes using a set-like interface
>>> el = fragment_fromstring('<span class="foo bar"></span>')
>>> 'foo' in el.classes
True
>>> 'f00' in el.classes
False
>>> el.classes.update(('qux', 'quux'))
>>> sorted(el.get('class').split())
['bar', 'foo', 'quux', 'qux']
>>> el.classes.clear()
>>> el.get('class')
>>> list(el.classes)
[]
>>> el.classes.add('a')
>>> el.classes.add('b')
>>> el.classes.remove('a')
>>> el.classes.remove('c')
Traceback (most recent call last):
...
KeyError: 'c'
>>> el.classes.discard('c')
>>> el.get('class')
'b'
>>> el.classes.add('b')
>>> el.get('class')
'b'
>>> el.classes |= ('a', 'b')
>>> el.get('class')
'b a'
>>> el.classes -= ('b', 'c', 'd')
>>> el.get('class')
'a'
with an extra toggle method to switch the state of classes
>>> el.get('class')
'a'
>>> el.classes.toggle('a')
False
>>> el.get('class')
>>> el.classes.toggle('foo')
True
>>> el.get('class')
'foo'
>>> el.classes.toggle('foo')
False
>>> el.get('class')
>>> el.classes.add("foo\n")
Traceback (most recent call last):
...
ValueError: Invalid class name: 'foo\n'
>>> el.classes.remove("foo ")
Traceback (most recent call last):
...
ValueError: Invalid class name: 'foo '
Also added is a get_rel_links, which you can use to search for links
like ``<a rel="$something">``::
>>> h = document_fromstring('''
... <a href="1">test 1</a>
... <a href="2" rel="tag">item 2</a>
... <a href="3" rel="tagging">item 3</a>
... <a href="4" rel="TAG">item 4</a>''')
>>> print([e.attrib['href'] for e in h.find_rel_links('tag')])
['2', '4']
>>> print([e.attrib['href'] for e in h.find_rel_links('nofollow')])
[]
Another method is ``get_element_by_id`` that does what it says::
>>> print(tostring(fragment_fromstring('''
... <div>
... <span id="test">stuff</span>
... </div>''').get_element_by_id('test'), encoding=unicode))
<span id="test">stuff</span>
Or to get the content of an element without the tags, use text_content()::
>>> el = fragment_fromstring('''
... <div>This is <a href="foo">a <b>bold</b> link</a></div>''')
>>> el.text_content()
'This is a bold link'
Or drop an element (leaving its content) or the entire tree, like::
>>> doc = document_fromstring('''
... <html>
... <body>
... <div id="body">
... This is a <a href="foo" id="link">test</a> of stuff.
... </div>
... <!-- a comment -->
... <div>footer</div>
... </body>
... </html>''')
>>> doc.get_element_by_id('link').drop_tag()
>>> print(tostring(doc, encoding=unicode))
<html>
<body>
<div id="body">
This is a test of stuff.
</div>
<!-- a comment -->
<div>footer</div>
</body>
</html>
>>> doc.get_element_by_id('body').drop_tree()
>>> print(tostring(doc, encoding=unicode))
<html>
<body>
<!-- a comment -->
<div>footer</div>
</body>
</html>
Note, however, that comment text will not be merged into the tree when you
drop the comment. Here, ``drop_tag()`` behaves exactly like ``drop_tree()``:
>>> for comment in doc.getiterator(Comment):
... comment.drop_tag()
>>> print(tostring(doc, encoding=unicode))
<html>
<body>
<div>footer</div>
</body>
</html>
In Python3 it should be possible to parse strings given as bytes objects, at
least if an encoding is given.
>>> from lxml.html import HTMLParser
>>> enc = 'utf-8'
>>> html_parser = HTMLParser(encoding=enc)
>>> src = '<html><body>Test</body></html>'.encode(enc)
>>> doc = fromstring(src, parser=html_parser)
>>> print(tostring(doc, encoding=unicode))
<html><body>Test</body></html>
>>> docs = fragments_fromstring(src, parser=html_parser)
>>> len(docs)
1
>>> print(docs[0])
Test
Bug 599318: Call fromstring with a frameset fragment should not raise an error,
the whole document is returned.
>>> import lxml.html
>>> content='''
... <frameset>
... <frame src="main.php" name="srcpg">
... </frameset>'''
>>> etree_document = lxml.html.fromstring(content)
>>> print(tostring(etree_document, encoding=unicode))
<html><frameset><frame src="main.php" name="srcpg"></frameset></html>
Bug 599318: Call fromstring with a div fragment should not raise an error,
only the element is returned
>>> import lxml.html
>>> content='<div></div>'
>>> etree_document = lxml.html.fromstring(content)
>>> print(tostring(etree_document, encoding=unicode))
<div></div>
Bug 599318: Call fromstring with a head fragment should not raise an error,
the whole document is returned.
>>> import lxml.html
>>> content='<head></head>'
>>> etree_document = lxml.html.fromstring(content)
>>> print(tostring(etree_document, encoding=unicode))
<html><head></head></html>
Bug 690319: Leading whitespace before doctype declaration should not raise an error.
>>> import lxml.html
>>> content='''
... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
... <html>
... </html>'''
>>> etree_document = lxml.html.fromstring(content)
>>> print(tostring(etree_document, encoding=unicode))
<html></html>
Feature https://github.com/lxml/lxml/pull/140: ensure_head_body option:
>>> from lxml.html import document_fromstring, tostring
>>> from functools import partial
>>> tos = partial(tostring, encoding=unicode)
>>> print(tos(document_fromstring('<p>test</p>')))
<html><body><p>test</p></body></html>
>>> print(tos(document_fromstring('<p>test</p>', ensure_head_body=True)))
<html><head></head><body><p>test</p></body></html>
>>> print(tos(document_fromstring('<meta>')))
<html><head><meta></head></html>
>>> print(tos(document_fromstring('<meta>', ensure_head_body=True)))
<html><head><meta></head><body></body></html>
>>> print(tos(document_fromstring('<html></html>')))
<html></html>
>>> print(tos(document_fromstring('<html></html>', ensure_head_body=True)))
<html><head></head><body></body></html>