文字列から URL を削除し、元のコンテンツのタイトルに置き換えたいと思います。
例えば:
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"
URL をタイトルに置き換えるために、次のスニペットを作成しました。
#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
この関数を文字列に適用して、URL をキャッチし、get_title を介してタイトルに変換する必要があります。