usr bin env python3 import sys from lxml html import fromstring tostri

 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
#!/usr/bin/env python3
import sys
from lxml.html import fromstring, tostring
def main():
data = sys.stdin.read()
tree = fromstring(data.encode('utf-8'))
try:
query = sys.argv[1]
except IndexError:
sys.stderr.write('Usage: xpath <xpath>')
sys.exit(1)
else:
for elem in tree.xpath(query):
if isinstance(elem, str):
print(elem)
else:
print(
tostring(elem, pretty_print=True, encoding='utf-8')
.decode('utf-8').strip()
)
print('-' * 10)
if __name__ == '__main__':
main()