#-*-Coding:utf-8-*-
__author__ = ' Administrator '
From PYQT4.QT Import *
From Pyqt4.qtcore Import *
From Pyqt4.qtgui Import *
Import Sys,os,datetime,urllib,urllib2,re,threading,thread
From G1 import Ui_form
Class Gui2 (Qdialog,ui_form):
def __init__ (self):
Super (Gui2,self). __init__ ()
Self.setupui (self)
Self. Urlcount.setreadonly (True)
T=qtimer (self)
T.timeout.connect (Self.datetimes)
T.start (1000)
Self.pushSelect.clicked.connect (self. FilePath)
Self. Urlfile.settext (R ' \pic ')
Self. DownButton.clicked.connect (Download)
def getwebsite (self):
return self. Urledit.text () #获取网址的函数
def getfilepath (self):
return self. Urlfile.text () #获取文件路径的函数
def settag (Self,downloadtag):
Self. Urlcount.settext (Downloadtag) #显示状态
def FilePath (self):
Filepath=qfiledialog.getexistingdirectory (Self, ' select ', '/pic ')
Self. Urlfile.settext (Unicode (filepath))
While True:
if (Filepath.isempty ()):
Qmessagebox.information (self, ' error ', ' ERROR ')
Filepath=qfiledialog.getexistingdirectory (Self, ' select ', '/')
Continue
Else:self. Urlfile.settext (Unicode (filepath))
Break
def datetimes (self):
Self. Urltime.settext (Datetime.datetime.now (). Strftime ('%y-%m-%d%h:%m:%s '))
def download ():
Wb=unicode (Main.getwebsite (), ' utf-8 '). Encode (' Utf-8 ')
Fp=main.getfilepath ()
Main.settag (' OK ')
T=MYT (WB,FP)
T.start ()
Class MyT (threading. Thread):
def __init__ (self,web,files):
Threading. Thread.__init__ (self)
Self.web=web
Self.files=files
def run (self):
Html=gethttl (Self.web)
Print Self.web
Print Self.files
Getimg (Html,self.files)
Main.settag (' OK ')
def gethttl (URL):
return Urllib.urlopen (URL). Read () #返回网页源码
def getimg (Html,path):
Reg=re.compile (R ' src= "(. *?\.) ( JPG|GIF|PNG|JS)) ')
Imglist=reg.findall (HTML)
Print Len (imglist)
X=1
For Imgurl in Imglist:
Print Imgurl
Main.settag (str (x) + '/' +str (len (imglist))) #在状态栏上显示进度
If imgurl[1]== ' gif ':
xpath=path+ ' \%d.gif '% x
Urllib.urlretrieve (Imgurl[0],xpath)
Elif imgurl[1]== ' png ':
xpath=path+ ' \%d.png '% x
Urllib.urlretrieve (Imgurl[0],xpath)
Elif imgurl[1]== ' js ':
xpath=path+ ' \%d.js '% x
Urllib.urlretrieve (Imgurl[0],xpath)
Else
xpath=path+ ' \%d.jpg '% x
Urllib.urlretrieve (Imgurl[0],xpath)
X+=1
print ' Finish--------'
App=qapplication (SYS.ARGV)
Main=gui2 ()
Main.show ()
Sys.exit (App.exec_ ())
Files: Http://url.cn/XDattY
Python bulk Download