python批量下載

來源:互聯網
上載者:User

標籤:

# -*- 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批量下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.