Python: replace urls with title names from a string
- by Hellnar
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