Extract Webpage Title from URL

A quick one-liner to pull the <title> tag from a remote page using Python’s requests and BeautifulSoup. This is useful for reconnaissance, bookmarking, or quickly verifying what a URL actually serves.

One-liner

python3 -c "import bs4, requests; print(bs4.BeautifulSoup(requests.get('$URL').text).title.text)"

Dependencies

On Debian-based systems:

sudo apt install python3-bs4 python3-requests

Why .text instead of .content

Using .text returns a Unicode string, while .content returns raw bytes. Piping raw bytes into other shell tools can mangle non-ASCII characters, so .text is the safer choice for interoperability.

Limitations

This does not work on pages that set the <title> via JavaScript after initial page load (e.g., Twitter/X, many SPAs). For those, you need a headless browser or a tool like wkhtmltoimage / puppeteer that executes JS.

Alternative: curl + sed

If Python is unavailable:

curl -s "$URL" | sed -n 's:.*<title>\(.*\)</title>.*:\1:p'

This is brittle (fails on multi-line titles, attributes in the tag, etc.) but works in a pinch.

Sources

Related: curl-jq-web-apis, python, xss-attacks