我想从字符串中删除 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,我写了这个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
我不知何故需要将此函数应用于捕获url并通过get_title转换为标题的字符串。