# SPDX-License-Identifier: Apache-2.0 """Read a page: open it in a read tab (it can look but never write), print its text, headings and links. pip install webpilot-si export WEBPILOT_URL=http://localhost:8931 WEBPILOT_TOKEN=cbu_... python examples/python/read_page.py # Wikipedia's article on web browsers python examples/python/read_page.py https://example.com Page content is untrusted data: this script prints it and never acts on instructions found in it. """ from __future__ import annotations import sys from webpilot import WebPilot url = sys.argv[1] if len(sys.argv) > 1 else "https://en.wikipedia.org/wiki/Web_browser" with WebPilot() as wp: # WEBPILOT_URL and WEBPILOT_TOKEN from the environment tab = wp.open(url) # mode="read" is the default try: snap = tab.snapshot() print(f"{tab.id}: {snap.title} ({snap.url})") print(f"{len(snap.elements)} interactive elements, e.g.:") for e in snap.elements[:5]: print(f" {e.ref:>5} {e.role:<10} {e.name[:60]}") print("\n" + tab.read_text()[:800]) meta = tab.extract("meta") print("\nheadings:", *meta.data.headings[:8], sep="\n ") links = tab.extract("links", limit=200) print(f"\n{len(links.data)} links; the first three:") for link in links.data[:3]: print(f" {link.text[:40]!r} -> {link.href}") finally: tab.close() # tabs outlive the client: close the ones you opened