# -*- coding: utf-8 -*- import re,urllib def tiny_url(url): """å°†url转æ¢æˆtinyurl""" apiurl = "http://tinyurl.com/api-create.php?url=" tinyurl = urllib.urlopen(apiurl + url).read() return tinyurl def content_tiny_url(content): """让消æ¯é‡Œé¢çš„连接转æ¢æˆæ›´çŸçš„Tinyurl""" regex_url = r'http:\/\/([\w.]+\/?)\S*' for match in re.finditer(regex_url, content): url = match.group(0) content = content.replace(url,tiny_url(url)) return content