python做一個簡易圖片下載工具
阿新 • • 發佈:2018-11-01
star pil release eve earch += error ear 信息
代碼有點亂,先這樣
# -*- coding:utf-8 -*- #__author__ :kusy #__content__:文件說明 #__date__:2018/11/01 11:01 import urllib.request, urllib.parse, urllib.error import os import re import time import threading # fileno = 0 # lock = threading.Lock() def mkdir(dir): cd = os.path.abspath(dir)if not os.path.exists(cd): os.mkdir(cd) def gethtml(url): page = urllib.request.urlopen(url) html = page.read() # print(html) return html def getimage(html, searchwords): reg = r‘"img":"(http:.*?\.jpg)"‘ imgre = re.compile(reg) imglist = re.findall(imgre,html.decode(‘utf-8‘)) # print(imglist) index_start = 0 splitcount = round(len(imglist) / 4) threadlist = [] # 開4個線程,分開同時下載 for t in range(4): if index_start + splitcount > len(imglist): imglist_part = imglist[index_start:] else: imglist_part= imglist[index_start:index_start + splitcount] index_start += splitcount threadlist.append(threading.Thread(target=download, args=(imglist_part, searchwords, t + 1))) for th in threadlist: th.start() for th in threadlist: th.join() def download(imglist,searchwords,threading_no): # global fileno fileno = 0 for imgurl in imglist: imgurl = imgurl.replace(‘\\‘,‘‘) # lock.acquire() try: filename = searchwords + ‘-‘ + str(threading_no) + ‘-‘ + str(fileno) # print(filename + ‘ 獲取中...‘) urllib.request.urlretrieve(imgurl,‘下載圖片/%s.jpg‘ % filename) # except (urllib.error.HTTPError, urllib.error.URLError): except Exception: continue # 打印信息放在這裏是因為,如果放在前面,當前請求異常時會重復打印該信息 print(filename + ‘ 獲取中...‘) fileno += 1 # lock.release() if __name__ == ‘__main__‘: mkdir(u‘下載圖片‘) searchwords = input(u‘請輸入搜索內容後回車 >>> ‘) print(u‘\n-----------------------文件存放在[下載圖片]目錄下-----------------------‘) # 指定圖片來源,這裏是360搜索,可以換成其他的 myurl = "http://image.so.com/i?q=" + urllib.parse.quote(searchwords) + "&src=srp" html = gethtml(myurl) getimage(html, searchwords) print(u‘\n-----------------------下載完畢,謝謝!!-----------------------‘) print(u‘\n-----------------------CopyRight @Kusy -----------------------‘) time.sleep(5)
執行效果
轉成exe文件,運行
python做一個簡易圖片下載工具