forked from brock7/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxpath.py
More file actions
executable file
·48 lines (38 loc) · 802 Bytes
/
xpath.py
File metadata and controls
executable file
·48 lines (38 loc) · 802 Bytes
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
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
#
# filename: xpath.py
# written by 老妖@wooyun
# date: 2014-06-06
#
###############################################################################
import sys
from lxml import etree
import types
import getopt
import locale
reload(sys)
sys.setdefaultencoding(locale.getpreferredencoding())
docType = 'HTML'
opts, args = getopt.getopt(sys.argv[1:], "x")
for op, vaule in opts:
if op == '-x':
docType = 'xml'
if len(args) < 1:
print sys.argv[0] + ' <xpath>'
sys.exit(-1)
text = ''
for line in sys.stdin:
text += line
if len(text) <= 0:
sys.exit(0)
if docType == 'HTML':
tree = etree.HTML(text)
else:
tree = etree.XML(text)
nodes = tree.xpath(args[0])
for node in nodes:
if hasattr(node, 'text'):
print node.text
else:
print node