这篇文章主要讲解了“怎么用Python调用短网址接口”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“怎么用Python调用短网址接口”吧!
API接口生成地址
腾讯短网址(
http://url.cn)
接口1:
http://www.sinadwz.cn/url/dwz.php?url_long=http://www.baidu.com
接口2:
http://maiyurl.cn/tengxurl?url_long=http://www.baidu.com
接口3:
http://www.sina-url.cn/txdwz.php?url_long=http://www.baidu.com
新浪短网址(
http://t.cn)
接口1:
http://www.sina-url.cn/xinlang?url_long=http://www.baidu.com
接口2:
http://www.qqdwz.cn/urlcn/api
?url_long=http://www.baidu.com
接口3:http://maiyurl.cn/weibourl?url_long=http://www.baidu.com
在线生成地址
腾讯短网址(
http://url.cn)
接口1:
http://www.sinadwz.cn
接口2:
http://maiyurl.cn
接口3:
http://www.sina-url.cn
新浪短网址(
http://t.cn)
接口1:
http://www.sina-url.cn
接口2:
http://www.qqdwz.cn
接口3:http://maiyurl.cn
注意事项:
① 调用api接口时,只需将 “http://www.baidu.com”换成需要缩短的长网址即可。
② 接口支持url参数,当url中出现 & 符号时,请用 %26 代替(或者使用url编码格式),否则参数可能会丢失。
③ 填写url时,必须要以http(s)://开头,否则可能会导致生出的短网址无法访问原网站。
④ 上文提到的几个url.cn短网址api接口,经测试都是比较稳定的,觉得好记得收藏一下,以免丢失。
常见问题:
① 长链接转换,为什么结尾的参数丢失了?
答:因为url中含有特殊字符,需要使用UTF8编码格式,将url编码
② 接口没有返回结果,是什么情况?
答:有些时候接口返回数据会有延迟,延时未返回则会提示生成失败;或者是因为原链接被封了。
短网址接口文档
PHP调用代码:
$url = 'http://www.baidu.com';
$api_url = ''.urlencode($url);
$short_url = file_get_contents($api_url);
echo $short_url;
JAVA调用代码:
public static void main(String path[]) throws Exception {
URL u = new URL("http%3A%2F%2Fwww.baidu.com");
InputStream in = u.openStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
byte buf[] = new byte[1024];
int read = 0;
while ((read = in .read(buf)) > 0) {
out.write(buf, 0, read);
}
} finally {
if ( in != null) {
in .close();
}
}
byte b[] = out.toByteArray();
System.out.println(new String(b, "utf-8"));
}
Python调用代码:
import urllib, urllib2, sys
host = ''
path = ''
method = 'GET'
querys = 'http%3A%2F%2Fwww.baidu.com'
bodys = {}
url = host + path + '?' + querys
request = urllib2.Request(url)
response = urllib2.urlopen(request)
content = response.read()
if (content):
print(content)
感谢各位的阅读,以上就是“怎么用Python调用短网址接口”的内容了,经过本文的学习后,相信大家对怎么用Python调用短网址接口这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是天达云,小编将为大家推送更多相关知识点的文章,欢迎关注!