#coding=gbk
from Tkinter import *
import pycurl
import sys, re, md5, os, time, commands
import pycurl
import cStringIO as _StringIO
import sys
import shutil, urllib, urllib2
import socket,sqlite3 as sqlite
import datetime, time
from time import sleep
site='12530'
# MIME type
accept_type = "*/*"
# http transfer limits
connection_timeout = 500
timeout = 1000
low_speed = 200
low_speed_time = 120
max_size = 20485760 # 10MB
root=Tk()
cwd=StringVar(root)
mobile=StringVar(root)
#three=StringVar(root)
text=Entry(root,width=50,textvariable=cwd)
l1=Label(root,text=u"请输入手机号码")
#textmobile=Entry(root,width=50,textvariable=mobile)
textmobile=Text(root,width=50,height=15)
l2=Label(root,text=u"请输入三位数字")
three=Text(root,width=50,height=5)
#text.pack()
l1.pack()
textmobile.pack()
l2.pack()
three.pack()
bfm=Frame(root)
t=Text(root,height=10,background='black',foreground='green')
def dols():
m=textmobile.get('0.0',END)
m=m.strip()
ss=m.split('\n')
ths=three.get('0.0',END)
ths=ths.strip('\n')
sth=ths.split('\n')
count=0
for th in sth:
for s in ss:
if len(s.strip()) == 0 : continue
t.insert('0.0', u"使用号码%s和%s抓取\n" % (s.strip(),th))
print url
v,c,type=get(url,count)
t.update()
sleep(1)
count=count+1
t.insert('0.0',u"抓取已完成,请换批号码再提交\n")
ok=Button(bfm,text=u'提交',activeforeground='white',
activebackground='green',command=dols)
ok.pack()
bfm.pack()
t.pack()
def get_curl(user_agent="MSIE"):
"initialize curl handle"
curl_handle = pycurl.Curl()
curl_handle.setopt(pycurl.FOLLOWLOCATION, 1)
curl_handle.setopt(pycurl.MAXREDIRS, 5)
curl_handle.setopt(pycurl.CONNECTTIMEOUT, connection_timeout)
curl_handle.setopt(pycurl.TIMEOUT, timeout)
curl_handle.setopt(pycurl.NOSIGNAL, 1)
curl_handle.setopt(pycurl.LOW_SPEED_LIMIT, 100)
curl_handle.setopt(pycurl.LOW_SPEED_TIME, low_speed_time)
curl_handle.setopt(pycurl.HTTPHEADER, ["User-Agent: %s" % user_agent, accept_type])
curl_handle.setopt(pycurl.MAXFILESIZE, max_size)
#curl_handle.setopt(pycurl.PROXY, "10.0.0.172")
#curl_handle.setopt(pycurl.PROXYPORT,80)
#curl_handle.setopt(PROXYUSERPWD,"dummyuser:dummypasswd")
return curl_handle
def curl_fetch(curl_handle, url,count):
"retrieve url, return the content, http code, time, effective url"
fp = _StringIO.StringIO()
curl_handle.setopt(pycurl.URL, url)
curl_handle.setopt(pycurl.WRITEFUNCTION, fp.write)
# perform the transfer
try:
curl_handle.perform()
except pycurl.error, e:
print e
return (-1, "", 0)
content_type = curl_handle.getinfo(pycurl.CONTENT_TYPE)
#print curl_handle.getinfo(pycurl.HTTP_CODE)
#t.insert('0.0', u"第 %d 次:Download speed: %.2f bytes/second\n" % (count,curl_handle.getinfo(curl_handle.SPEED_DOWNLOAD)) )
t.insert('0.0',content_type)
t.insert('0.0',u"大小是%s字节\n" %(len(fp.getvalue())) )
t.update()
print content_type
return (0,fp.getvalue(), content_type)
def get(url,count):
handle=get_curl()
v,c,t=curl_fetch(handle,url,count)
handle.close()
return(v,c,t)
root.mainloop()
Tkinter图形界面设计
最新推荐文章于 2024-04-05 22:49:40 发布