-
Notifications
You must be signed in to change notification settings - Fork 0
/
xpath_soup.py
45 lines (39 loc) · 1.21 KB
/
xpath_soup.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
# Author: ergoithz
# https://gist.github.com/ergoithz/6cf043e3fdedd1b94fcf
#!/usr/bin/python
# -*- coding: utf-8 -*-
def xpath_soup(element):
"""
Generate xpath from BeautifulSoup4 element
:param element: BeautifulSoup4 element.
:type element: bs4.element.Tag or bs4.element.NavigableString
:return: xpath as string
:rtype: str
Usage:
>>> import bs4
>>> html = (
... '<html><head><title>title</title></head>'
... '<body><p>p <i>1</i></p><p>p <i>2</i></p></body></html>'
... )
>>> soup = bs4.BeautifulSoup(html, 'html.parser')
>>> xpath_soup(soup.html.body.p.i)
'/html/body/p[1]/i'
"""
components = []
child = element if element.name else element.parent
for parent in child.parents:
"""
@type parent: bs4.element.Tag
"""
siblings = parent.find_all(child.name, recursive=False)
components.append(
child.name
if siblings == [child] else
'%s[%d]' % (child.name, 1 + siblings.index(child))
)
child = parent
components.reverse()
return '/%s' % '/'.join(components)
if __name__ == '__main__':
import doctest
doctest.testmod(verbose=True)