Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This script is for use with Keyboard Maestro, as described at: It outputs the title of a webpage. The URL is passed via a Keyboard Maestro variable. This is a rather crude, but reliable, method of getting the title and does not require the installation o…
#! /usr/bin/python
brute force get title of web page
usage: url
Gordon Meyer January 1, 2013
# modules used
import urllib2, urlparse, string, os
def getPage(theURL):
## returns the HTML text of the URL
req = urllib2.Request(theURL)
thePage = urllib2.urlopen(req)
theHTML =
return theHTML
def extractTitle(theLine):
## finds and returns contents of <title> tag
titleStart = theLine.find("<title>")
if titleStart > -1: #make sure we found the tag, -1 will mean we did not
titleStart = titleStart + 7 #skip to end of tag
titleEnd = theLine.find("</title>")
theTitle = theLine[titleStart:titleEnd]
theTitle = theTitle.strip() # remove whitespace
theTitle = ""
# get URL from Keyboard Maestro variable
theURL = os.getenv('KMVAR_theURL')
thePageHTML = getPage(theURL)
theTitle = extractTitle(thePageHTML)
print theTitle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment