-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextract.py
More file actions
30 lines (28 loc) · 823 Bytes
/
extract.py
File metadata and controls
30 lines (28 loc) · 823 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
def extract(begin, end, html):
if not html:
return ''
start = html.find(begin)
if start >= 0:
start += len(begin)
if end is not None:
end = html.find(end, start)
if end is None or end >= 0:
return html[start:end].strip()
def extract_all(begin, end, html):
return map(str.strip, _extract_all(begin, end, html))
def _extract_all(begin, end, html):
if not html:
return ''
result = []
from_pos = 0
while True:
start = html.find(begin, from_pos)
if start >= 0:
start += len(begin)
endpos = html.find(end, start)
if endpos >= 0:
result.append(html[start:endpos])
from_pos = endpos+len(end)
continue
break
return result