短URL.py:在Python中,从comand行短取一个长URL
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了短URL.py:在Python中,从comand行短取一个长URL相关的知识,希望对你有一定的参考价值。
#!/usr/bin/env python # # shorturl.py # Shorts a long URL from command line, using ur1.ca. # Shows original page's title and short url. # # ksaver, (at identi.ca); Sep, 2010. # version 0.2, Mar 2011. # Requieres BeautifulSoup library in order to work properly: # http://www.crummy.com/software/BeautifulSoup/ # # Public Domain Code. import sys import urllib2 from BeautifulSoup import BeautifulSoup from urllib import urlencode def shorten(longurl): shortener = 'http://ur1.ca/' urlparam = {'longurl': longurl} encparam = urlencode(urlparam) request = urllib2.Request(shortener, encparam) htmlsoup = BeautifulSoup(htmlpage) txtmatch = htmlsoup.p.text.find('http') shorturl = htmlsoup.p.text[txtmatch:] return shorturl def main(url): htmldoc = urllib2.urlopen(url).read() mysoup = BeautifulSoup(htmldoc) title = mysoup.title.text shorturl = shorten(url) print "'%s': %s\n" % (title, shorturl) if __name__ == '__main__': if len(sys.argv) > 1: url = sys.argv[1] else: url = raw_input("URL to short: ") main(url)
以上是关于短URL.py:在Python中,从comand行短取一个长URL的主要内容,如果未能解决你的问题,请参考以下文章
Python爬虫实战:利用scrapy,短短50行代码下载整站短视频