# -*- coding: utf-8 -*-
"""打印指定 URL 的全部 href（去重）+ 关键词上下文。"""
import sys, re, urllib.request, gzip, io, ssl
url = sys.argv[1]
kw = sys.argv[2] if len(sys.argv) > 2 else ''
req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'})
ctx = ssl.create_default_context(); ctx.check_hostname = False; ctx.verify_mode = ssl.CERT_NONE
raw = urllib.request.urlopen(req, timeout=30, context=ctx).read()
if raw[:2] == b'\x1f\x8b':
    raw = gzip.decompress(raw)
html = raw.decode('utf-8', 'ignore')
hrefs = re.findall(r'href="([^"]+)"', html)
uniq = []
for h in hrefs:
    if h not in uniq:
        uniq.append(h)
print('total href', len(hrefs), 'unique', len(uniq))
for h in uniq:
    if not kw or kw.lower() in h.lower():
        print(h)
