37,743
社区成员




def downloadImg(url_list,path,start,end,ip_obj):
print('thread %s is running...' % threading.current_thread().name)
ss = requests.session()
proxies = {ip_obj.type:"http://%s:%s" % (ip_obj.ip,ip_obj.port)}
thread_name = threading.current_thread().name
img_count = 0
for img_url in url_list[start:end]:
#time.sleep(1)
print "%s正在下载%d/%d张图" % (thread_name,img_count,end - start)
img_content = ss.get(img_url,proxies = proxies)
name = img_url.split(".")
with open(os.path.join(path,thread_name + "_"+str(img_count) +"."+name[-1]),'wb') as f:
f.write(img_content.content)
img_count = img_count + 1
time.sleep(5)
print('thread %s ended.' % threading.current_thread().name)
thread_count = 0
thread = []
interval = len(url_list) / 30
start = 0
end = interval
while start < len(url_list):
t = threading.Thread(target=downloadImg,args=(url_list,path,start,end,ip_list[thread_count]),name="Thread" + str(thread_count))
thread.append(t)
start = end
end = end + interval
thread_count = thread_count + 1
for t in thread:
t.start()
for t in thread:
t.join()