Skip to content

Instantly share code, notes, and snippets.

@teemuy
Created October 23, 2019 18:14
Show Gist options
  • Save teemuy/81b1562db324f86acb024d00c2fe3564 to your computer and use it in GitHub Desktop.
Save teemuy/81b1562db324f86acb024d00c2fe3564 to your computer and use it in GitHub Desktop.
Fetch HTML title given page URL, using requests and lxml
#!/usr/bin/env python
"""Fetch HTML title for URL"""
import sys
import requests
import lxml.html as html
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Fetches HTML title for given URL.\n\nUsage: %s <URL>' % sys.argv[0])
sys.exit(1)
req = requests.get(sys.argv[1])
doc = html.fromstring(req.content)
title = doc.xpath('head/title')[0]
print(title.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment