Categories
Ruby tools

编码生成新浪微博短链接

短链接是个好东西,用在短信里省了不少文字。新浪微博API提供短链接的接口,但要申请weibo appkey ,一下子搞清楚什么是oAuth2.0,估计折腾一段时间。有现成的网页转换地址 http://www.henshiyong.com/tools/sina-shorten-url.php ,在此基础上修改即可。

用浏览器查看 的代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset="UTF-8" />
<title>在线版新浪微博短链接生成器</title>
<meta name="description" content="短链接生成器,新浪微博短连接生成器,在线版新浪微短连接生成,方便快捷完成您的URL转换!" />
<meta name="keywords" content="短链接生成器,新浪微博短连接生成器,在线版新浪微短连接生成" />
<style type="text/css">
textarea{border:5px solid;border-radius:8px 8px 0 0;width:400px;padding:8px;}
body{text-align:center;margin:150px auto;}
</style>
</head>
<body>
<h2>请输入你需要转换的网页地址</h2>
<form action="" method="post">
<input type="" name="url" style="width:400px"/><br/>
<input type="submit" value="转换" name="submit">
</form>
</body>
</html>

小部队先上,用Ruby写个简单脚本,发 POST 请求。

[deli@violet ~]$ ruby sina-shorten-url.rb

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset="UTF-8" />
<title>在线版新浪微博短链接生成器</title>
<meta name="description" content="短链接生成器,新浪微博短连接生成器,在线版新浪微短连接生成,方便快捷完成您的URL转换!" />
<meta name="keywords" content="短链接生成器,新浪微博短连接生成器,在线版新浪微短连接生成" />
<style type="text/css">
textarea{border:5px solid;border-radius:8px 8px 0 0;width:400px;padding:8px;}
body{text-align:center;margin:150px auto;}
</style>
</head>
<body>
<h2>新浪短连接已经生成:</h2><textarea>http://t.cn/h51yw</textarea><h2>继续转换</h2></body>
</html>

提取 textarea 标签数据即可,使用正则表达式最方便的了。

If you enjoyed this post, make sure you subscribe to my RSS feed!

Leave a Reply

Your email address will not be published. Required fields are marked *