国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python多線程下載文件的方法

2019-11-25 17:12:30
字體:
來源:轉載
供稿:網友

本文實例講述了Python多線程下載文件的方法。分享給大家供大家參考。具體實現方法如下:

import httplibimport urllib2import timefrom threading import Threadfrom Queue import Queuefrom time import sleepproxy = '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 = 5failedId = [];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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巫山县| 车险| 屯昌县| 民权县| 乌拉特后旗| 长葛市| 宜宾市| 松桃| 普安县| 南京市| 石台县| 拉萨市| 津市市| 正宁县| 桃园市| 梓潼县| 威宁| 临夏县| 吴川市| 普兰县| 射阳县| 泰宁县| 留坝县| 滕州市| 曲靖市| 常州市| 宾川县| 三河市| 齐齐哈尔市| 比如县| 惠州市| 浦城县| 滨海县| 平乡县| 昔阳县| 龙泉市| 开江县| 陵川县| 柳河县| 班玛县| 鲜城|