"""Print a news page's dates, title and paragraph text, read as data (no scripts run). usage: python3 -I article_text.py [max chars]""" import sys, re, html, json sys.stdout.reconfigure(encoding="utf-8") s = open(sys.argv[1], "rb").read().decode("utf-8", "replace") lim = int(sys.argv[2]) if len(sys.argv) > 2 else 6000 for pat in (r']+(?:property|name|itemprop)="(article:published_time|article:modified_time|og:title|og:description|datePublished|dateModified|publish-date|date|og:updated_time)"[^>]+content="([^"]*)"', r']+content="([^"]*)"[^>]+(?:property|name|itemprop)="(article:published_time|article:modified_time|og:title|datePublished)"'): for m in re.finditer(pat, s, re.I): print("META", m.group(1), "=", html.unescape(m.group(2))[:300]) for m in re.finditer(r'"(datePublished|dateModified|uploadDate)"\s*:\s*"([^"]+)"', s): print("JSONLD", m.group(1), "=", m.group(2)) for m in re.finditer(r"]*datetime=\"([^\"]+)\"[^>]*>(.*?)", s, re.I | re.S): print("TIME", m.group(1), "|", re.sub(r"<[^>]+>", "", m.group(2)).strip()[:80]) t = re.search(r"]*>(.*?)", s, re.I | re.S) print("TITLE", html.unescape(t.group(1)).strip() if t else "") body = re.sub(r"(?is)<(script|style|noscript|svg|iframe)[^>]*>.*?", " ", s) art = re.search(r"(?is)]*>(.*?)", body) zone = art.group(1) if art else body paras = re.findall(r"(?is)<(?:p|h1|h2|h3|li|figcaption)[^>]*>(.*?)", zone) out = [] for p in paras: txt = html.unescape(re.sub(r"<[^>]+>", "", p)).strip() txt = re.sub(r"\s+", " ", txt) if len(txt) > 25: out.append(txt) text = "\n".join(out) print("---- TEXT (%d chars) ----" % len(text)) print(text[:lim])