[範例] tinyurl + clipboard

看板Python作者 (愚人)時間18年前 (2006/12/07 21:13), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串1/1
#!/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
test in linux
12/07 21:14, 1F
文章代碼(AID): #15U1E681 (Python)
文章代碼(AID): #15U1E681 (Python)