-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetpage
executable file
·63 lines (50 loc) · 1.65 KB
/
getpage
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
#!/usr/bin/env python2
# -*- mode:python -*-
import sys # for handling arguments (argv)
import libxml2
import urllib2
def namespaceDefs(node):
n = node.nsDefs()
while n:
yield n
n = n.next
def checkNamespaceDefs(node, count):
nsList = list(namespaceDefs(node))
#print nsList
if len(nsList) != count :
raise Exception("Error: saw %d namespace declarations. Expected %d" % (len(nsList), count))
def printNamespaceDefs(node):
nsList = list(namespaceDefs(node))
print nsList
def read_url(f_use):
if len(sys.argv) > 1:
xmlpath = sys.argv[1]
try:
response = urllib2.urlopen(xmlpath)
f_use(response)
except urllib2.URLError as e:
sys.stderr.write("Failed to open URL \"%s\"\n\"%s\"\n"
% (xmlpath, e.reason))
def write_xmldoc(rfile):
readopts = (libxml2.HTML_PARSE_RECOVER |
libxml2.HTML_PARSE_NODEFDTD |
libxml2.HTML_PARSE_NOERROR |
libxml2.HTML_PARSE_NOWARNING |
libxml2.HTML_PARSE_NONET)
xmldoc = libxml2.htmlReadDoc(rfile.read(),None,None,readopts)
if xmldoc:
dumpopts = (libxml2.XML_SAVE_FORMAT +
libxml2.XML_SAVE_NO_DECL +
libxml2.XML_SAVE_XHTML +
libxml2.XML_SAVE_AS_XML
)
root = xmldoc.getRootElement()
root.saveTo(sys.stdout, None, dumpopts)
# xmldoc.formatDump(sys.stdout,dumpopts)
xmldoc.freeDoc()
else:
sys.stderr.write("failed to parse html document")
def main():
read_url(write_xmldoc)
if __name__ == "__main__":
main()