28 lines
661 B
Python
28 lines
661 B
Python
"""Print HTML context around a keyword.
|
|
|
|
Usage: python ctx.py <url> <keyword> [window]
|
|
"""
|
|
import urllib.request
|
|
import sys
|
|
import re
|
|
|
|
UA = 'Mozilla/5.0 (compatible; paperbot/1.0)'
|
|
url = sys.argv[1]
|
|
kw = sys.argv[2]
|
|
win = int(sys.argv[3]) if len(sys.argv) > 3 else 600
|
|
req = urllib.request.Request(url, headers={'User-Agent': UA})
|
|
t = urllib.request.urlopen(req, timeout=40).read().decode('utf-8', 'ignore')
|
|
idx = 0
|
|
n = 0
|
|
while True:
|
|
i = t.find(kw, idx)
|
|
if i < 0:
|
|
break
|
|
n += 1
|
|
print('--- occurrence %d at %d ---' % (n, i))
|
|
print(t[max(0, i - win):i + win])
|
|
idx = i + 1
|
|
if n >= 5:
|
|
break
|
|
print('occurrences found:', n)
|