有趣的.py:从命令行短接url,使用有趣的服务
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了有趣的.py:从命令行短接url,使用有趣的服务相关的知识,希望对你有一定的参考价值。
None.
#!/ usr/bin/env python # # fun.py # short a url using http://fun.ly shortener service. # ksaver (at identi.ca) # Public Domain Code. import urllib import urllib2 import sys from BeautifulSoup import BeautifulSoup as cooksoup def shorten(longurl): uagent = 'Opera/9.80 (X11; FreeBSD 8.1-RELEASE i386; U; en) Presto/2.6.30 Version/10.62' headers = {'User-Agent': uagent} shortener = 'http://fun.ly/' webparams = {'funly': longurl} encparams = urllib.urlencode(webparams) urlreqst = urllib2.Request(shortener, encparams, headers) htmlpage = urllib2.urlopen(urlreqst).read() soup = cooksoup(htmlpage) shorturl = soup.findAll('p')[1].text return shorturl def main(argv): if len(argv) > 1: shorturl = shorten(argv[1]) else: longurl = raw_input("Give me a URL: ") shorturl = shorten(longurl) print "%s" % shorturl if __name__ == '__main__': main(sys.argv)
以上是关于有趣的.py:从命令行短接url,使用有趣的服务的主要内容,如果未能解决你的问题,请参考以下文章