[範例] tinyurl + clipboard
#!/usr/bin/env python
import pygtk
pygtk.require("2.0")
import gtk, gobject
from mechanize import Browser
# just copy url text into clipboard and run this script
# you will have a tinyurl
def make_tinyurl(url):
# make tiny url
br = Browser()
br.open('http://tinyurl.com')
br.select_form(name='f')
br['url']=url
resp = br.submit()
html = resp.read()
# parsing tiny url
import re
tpat = re.compile('href="(.*)" target="_blank"')
return tpat.search(html).groups()[0]
def cb_text_received(cb, text, data):
if text is None:
gtk.main_quit()
else:
import re
urlpat = re.compile('http')
is_url = urlpat.search(text)
if is_url is not None:
print "url: ", text
url = make_tinyurl(text)
print "tiny url: ", url
else:
print "not a vaild url"
gtk.main_quit()
if __name__ == '__main__':
cb = gtk.clipboard_get('CLIPBOARD')
cb.request_text(cb_text_received)
gtk.main()
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 163.26.34.20
→
12/07 21:14, , 1F
12/07 21:14, 1F
Python 近期熱門文章
PTT數位生活區 即時熱門文章