Python make url address to Tinyurl


/ Published in: Python
Save to your folder(s)

Python make url address to Tinyurl in article content like Twitter message


Copy this code and paste it in your HTML
  1. def tiny_url(url):
  2. """将url转换成tinyurl"""
  3. apiurl = "http://tinyurl.com/api-create.php?url="
  4. tinyurl = urllib.urlopen(apiurl + url).read()
  5. return tinyurl
  6.  
  7. def content_tiny_url(content):
  8. """让消息里面的连接转换成更短的Tinyurl"""
  9.  
  10. regex_url = r'http:\/\/([\w.]+\/?)\S*'
  11. for match in re.finditer(regex_url, content):
  12. url = match.group(0)
  13. content = content.replace(url,tiny_url(url))
  14.  
  15. return content

URL: http://huacn.blogbus.com/logs/26387436.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.