Python多線程下載文件的方法
本文實例講述了Python多線程下載文件的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
import httplib import urllib2 import time from threading import Thread from Queue import Queue from time import sleep proxy = 'your proxy'; opener = urllib2.build_opener( urllib2.ProxyHandler({'http':proxy}) ) urllib2.install_opener( opener ) ids = {}; for i in range(1,110): try: listUrl = "http://www.someweb.net/sort/list_8_%d.shtml" % (i); print listUrl; page = urllib2.urlopen(listUrl).read(); speUrl = "http://www.someweb.net/soft/"; speUrlLen = len(speUrl); idx = page.find(speUrl,0); while idx!=-1: dotIdx = page.find(".",idx + speUrlLen); if dotIdx != -1: id = page[idx + speUrlLen:dotIdx]; ids[id] = 1; idx = page.find("http://www.someweb.net/soft/",idx + speUrlLen); except: pass; q = Queue() NUM = 5 failedId = []; def do_somthing_using(id): try: url = "http://www.someweb.net/download.php?softid=%s&type=dx" % (id); h2 = httplib.HTTPConnection("your proxy", "you port"); h2.request("HEAD", url); resp = h2.getresponse(); header = resp.getheaders(); location = header[3][1]; sContent = urllib2.urlopen(location).read(); savePath = "C:\\someweb\\%s.rar" % (id); file=open(savePath,'wb'); file.write(sContent); file.close(); print savePath + " saved"; except: pass; def working(): while True: arguments = q.get() do_somthing_using(arguments) sleep(1) q.task_done() for i in range(NUM): t = Thread(target=working) t.setDaemon(True) t.start() for id in ids: q.put(id) q.join()
希望本文所述對大家的Python程序設(shè)計有所幫助。
- python實現(xiàn)單線程多任務非阻塞TCP服務端
- Python實現(xiàn)簡單的多任務mysql轉(zhuǎn)xml的方法
- python多任務及返回值的處理方法
- python多任務之協(xié)程的使用詳解
- Python協(xié)程操作之gevent(yield阻塞,greenlet),協(xié)程實現(xiàn)多任務(有規(guī)律的交替協(xié)作執(zhí)行)用法詳解
- python實現(xiàn)通過隊列完成進程間的多任務功能示例
- Python實現(xiàn)多線程下載文件的代碼實例
- 對Python多線程讀寫文件加鎖的實例詳解
- Python多線程同步---文件讀寫控制方法
- python 多線程將大文件分開下載后在合并的實例
- python開發(fā)之基于thread線程搜索本地文件的方法
- python多線程案例之多任務copy文件完整實例
相關(guān)文章
PHP網(wǎng)頁抓取之抓取百度貼吧郵箱數(shù)據(jù)代碼分享
本文給大家介紹PHP網(wǎng)頁抓取之抓取百度貼吧郵箱數(shù)據(jù)代碼分享,程序?qū)崿F(xiàn)了一鍵抓取帖子全部郵箱和分頁抓取郵箱兩個功能,感興趣的朋友一起學習吧2016-04-04Python系統(tǒng)監(jiān)控模塊psutil功能與經(jīng)典用法分析
這篇文章主要介紹了Python系統(tǒng)監(jiān)控模塊psutil功能與經(jīng)典用法,簡單講述了psutil模塊的功能、原理并結(jié)合具體實例形式分析了Python使用psutil模塊針對CPU、內(nèi)存、磁盤、網(wǎng)絡(luò)等信息的讀取相關(guān)操作技巧,需要的朋友可以參考下2018-05-05Python調(diào)用系統(tǒng)命令os.system()和os.popen()的實現(xiàn)
這篇文章主要介紹了Python調(diào)用系統(tǒng)命令os.system()和os.popen()的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12Python?copy()與deepcopy()方法之間有什么區(qū)別
這篇文章主要介紹了Python中的copy()和deepcopy(),下面詳細介紹該內(nèi)容并附上詳細代碼,需要的朋友可以參考一下文章的具體內(nèi)容,希望對你有所幫助2022-10-10Python Pillow.Image 圖像保存和參數(shù)選擇方式
今天小編就為大家分享一篇Python Pillow.Image 圖像保存和參數(shù)選擇方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01兩行Python代碼實現(xiàn)pdf轉(zhuǎn)word功能
這篇文章主要為大家詳細介紹了如何利用兩行Python代碼就能實現(xiàn)pdf轉(zhuǎn)word功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-03-03