python做一个简易图片下载工具

代码有点乱,先这样

# -*- 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文件,运行

 

posted on 2018-11-01 14:23  一個未来  阅读(957)  评论(0编辑  收藏  举报

导航