-
Notifications
You must be signed in to change notification settings - Fork 15
/
run_parslepy.py
34 lines (25 loc) · 1 KB
/
run_parslepy.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import optparse
import pprint
import parslepy
import lxml.html
def main():
parser = optparse.OptionParser()
parser.add_option("--debug", dest="debug", action="store_true", help="debug mode", default=False)
parser.add_option("--url", dest="url", help="fetch this URL", default=None)
parser.add_option("--file", dest="inputfile", help="parse this HTML file", default=None)
parser.add_option("--script", dest="parselet", help="Parsley script filename", default=None)
(options, args) = parser.parse_args()
if not options.parselet:
print("You must provide a Parsley script")
return
if not options.url and not options.inputfile:
print("You must provide an URL")
return
with open(options.parselet) as fp:
extractor = parslepy.Parselet.from_jsonfile(fp, options.debug)
output = extractor.parse(options.url or options.inputfile)
pprint.pprint(output)
if __name__ == '__main__':
main()