码迷,mamicode.com
首页 > 编程语言 > 详细

python批量下载

时间:2015-07-16 16:02:01      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

# -*- 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_())

 文件:http://url.cn/XDattY

python批量下载

标签:

原文地址:http://www.cnblogs.com/mhxy13867806343/p/4153479.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!