Python: replace urls with title names from a string

Posted by Hellnar on Stack Overflow See other posts from Stack Overflow or by Hellnar
Published on 2010-05-08T17:24:53Z Indexed on 2010/05/08 17:28 UTC
Read the original article Hit count: 210

Filed under:
|
|
|

Hello I would like to remove urls from a string replace them with their titles of the original contents.

For example:

mystring = "Ah I like this site: http://www.stackoverflow.com. Also I must say I like http://www.digg.com"

sanitize(mystring) # it becomes "Ah I like this site: Stack Overflow. Also I must say I like Digg - The Latest News Headlines, Videos and Images"

For replacing url to the title, I have written this snipplet:

#get_title: string -> string
def get_title(url):
    """Returns the title of the input URL"""

    output = BeautifulSoup.BeautifulSoup(urllib.urlopen(url))
    return output.title.string

© Stack Overflow or respective owner

Related posts about python

Related posts about url