-
Notifications
You must be signed in to change notification settings - Fork 0
/
html-find.py
44 lines (38 loc) · 921 Bytes
/
html-find.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
#!/usr/bin/python3
##
# This script helps to search in a html file. Currently it supports only searching for a value of a tag.
#
# Params:
# 1.: The HTML file.
# 2.: The result file.
# 3.: The name of the tag to search for.
##
# process args
import sys
pathHtml = sys.argv[1]
pathResult = sys.argv[2]
tag = sys.argv[3]
##
# search for the tag and log the values
##
from html.parser import HTMLParser
class TagHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.count = 0
def handle_starttag(self, start_tag, attrs):
if tag == start_tag:
self.count += 1
def handle_endtag(self, end_tag):
if tag == end_tag:
self.count -= 1
def handle_data(self, data):
if self.count > 0:
result.write(data)
htmlFile = open(pathHtml, 'r')
html = htmlFile.read()
htmlFile.close()
result = open(pathResult, 'w', True)
parser = TagHTMLParser()
parser.feed(html)
result.close()