Take the 2-minute tour ×
Dear Sam Adams

Thanks for your submission to ourdirectory.com
URL: http://myurlok.us
Please click below link to confirm your submission.

Once we receive your comfirmation, your site will be included for process!


Thank you!

Should be obvious which URL I need to extract.

share|improve this question
I'd be interested to know why you want to do this. What's it for? –  Mark Byers Nov 24 '09 at 19:32
surely an automated submission bot to some external site. –  Brian Schroth Nov 24 '09 at 19:33
Yes, I have like thousands of these emails coming in that need to be confirmed. –  Demon Labs Nov 24 '09 at 19:33

5 Answers 5

up vote 0 down vote accepted

This solution works only if the source is not HTML.

def extractURL(self,fileName):

    wordsInLine = []
    tempWord = []
    urlList = []

    #open up the file containing the email
    for line in file:
        #create a list that contains is each word in each line
        wordsInLine = line.split(' ')
        #For each word try to split it with :
        for word in wordsLine:
            tempWord = word.split(":")
            #Check to see if the word is a URL
            if len(tempWord) == 2:
                if tempWord[0] == "http":

    return urlList
share|improve this answer

If it's HTML email with hyperlinks you could use the HTMLParse library as a shortcut.

import HTMLParser
class parseLinks(HTMLParser.HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == 'a':
            for name, value in attrs:
                if name == 'href':
                    print value
                    print self.get_starttag_text()

someHtmlContainingLinks = ""
linkParser = parseLinks()
share|improve this answer
his sample document doesn't look like HTML to me :-) –  Suppressingfire Nov 25 '09 at 0:54
Everyone else provided non-HTML solutions, and the OP's question history indicates he's pulling it from gmail, which does support HTML. Given the vagueness of the question, I think this is a valid response. –  Matt Garrison Nov 30 '09 at 14:45

@OP, if your email is always standard,

for line in f:
    if "confirm your submission" in line:
        print f.next().strip()        
share|improve this answer

Not easy. One suggestion (taken from the RegexBuddy library):


will match URLs (without mailto:, if you want that, say so), even if they are enclosed in parentheses. Will also match URLs without http:// or ftp:// etc. if they start with www. or ftp..

A simpler version:


It all depends on what your needs are/what your input looks like.

share|improve this answer
I don't think you need to get so fancy. I think he wants to parse very specific emails from a very specific source, so I imagine he could parse for the exact string: "ourdirectory.com/confirm.aspx?id="; followed by digits and end-of-line. –  Brian Schroth Nov 24 '09 at 19:35
Probably yes. Although there is another URL (myurlok.us) in there. Who knows what else might turn up - he wasn't very specific. –  Tim Pietzcker Nov 24 '09 at 19:37



or without regex, parse the email line by line and test if the string contains "http://www.ourdirectory.com/confirm.aspx?id=" and if it does, that's your URL.

Of course, if your input is actually the HTML source instead of the text you posted, this all goes out the window.

share|improve this answer

Your Answer


