Using Python to Shorten a URL Using Google's Shortening Service
Using Python to shorten a URL with Google's shortening service (goo.gl):
02.# Corey Goldberg - 2010
03.
04.import json
05.import urllib
06.import urllib2
07.
08.
09.def shorten(url):
10.gurl = 'http://goo.gl/api/url?url=%s' % urllib.quote(url)
11.req = urllib2.Request(gurl, data='')
12.req.add_header('User-Agent', 'toolbar')
13.results = json.load(urllib2.urlopen(req))
14.return results['short_url']
15.
16.
17.if __name__ == '__main__':
18.print shorten('http://www.goldb.org/')
19.print shorten('www.yahoo.com')You give it a URL to shorten: shorten('http://www.goldb.org/long_url')
... and it returns a shortened URL for you: 'http://goo.gl/jh4W'
CODE:
01.#!/usr/bin/python02.# Corey Goldberg - 2010
03.
04.import json
05.import urllib
06.import urllib2
07.
08.
09.def shorten(url):
10.gurl = 'http://goo.gl/api/url?url=%s' % urllib.quote(url)
11.req = urllib2.Request(gurl, data='')
12.req.add_header('User-Agent', 'toolbar')
13.results = json.load(urllib2.urlopen(req))
14.return results['short_url']
15.
16.
17.if __name__ == '__main__':
18.print shorten('http://www.goldb.org/')
19.print shorten('www.yahoo.com')You give it a URL to shorten: shorten('http://www.goldb.org/long_url')
... and it returns a shortened URL for you: 'http://goo.gl/jh4W'
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-734053/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- google api , using a refresh token to get the access tokenGoAPI
- pdf crop using pythonPython
- git clone 報錯 “URL using bad/illegal format or missing URL“GitORM
- 【SQL】Using Oracle's Parallel Execution FeaturesSQLOracleParallel
- Coinhoarders Steal $50 Million in Cryptocurrency Using Google AdsGo
- Using index condition Using indexIndex
- zt_Using Oracle's Parallel Execution FeaturesOracleParallel
- MySQL 索引優化 Using where, Using filesortMySql索引優化
- using indexIndex
- MySQL explain結果Extra中"Using Index"與"Using where; Using index"區別MySqlAIIndex
- 9 check Palindrome Number by using pythonPython
- Using lxml.objectify to Parse XML With PythonXMLObjectPython
- Using hints for PostgresqlSQL
- MySQL 之 USINGMySql
- Using the WITH CHECK OPTION
- CQRS using phpPHP
- Using sub-generators for lexical scanning in PythonPython
- Example of SQL Linux Windows Authentication configuration using Managed Service AccountsSQLLinuxWindows
- 解決 ALL MIRROR URLS ARE NOT USING FTP, HTTP[S] OR FILE 問題FTPHTTP
- [Angular 2] Using a Reducer to Change an Object's Property Inside an ArrayAngularObjectIDE
- What are the benefits of using an proxy?
- Using mysqldump for backupsMySql
- A example that using JQuery clonejQuery
- Using MongoDB in C#MongoDBC#
- Using Oracle SecureFiles LOBsOracle
- USING NHIBERNATE WITH MySQLMySql
- Regression Analysis Using ExcelExcel
- Using NHibernate with SQLiteSQLite
- Using svn in CLI with BatchBAT
- Using dbms_monitor
- alter table using indexIndex
- Using Multiple Tablespaces (46)
- [Angular-Scaled web] 4. Using ui-router's named viewsAngularWebUIView
- 淺談Using filesort和Using temporary 為什麼這麼慢
- Using HiveServer2 - AuthenticationHiveServer
- Using Sorted Sets with Jedis APIAPI
- Using Multiple Variables with the Same Name
- Using AUTO_INCREMENT CASEREM