码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-11-01 15:09:57      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:内容   release   []   文件   cep   dir   filename   搜索   --   

代码有点乱,先这样

# -*- 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做一个简易图片下载工具

标签:内容   release   []   文件   cep   dir   filename   搜索   --   

原文地址:https://www.cnblogs.com/kusy/p/9889031.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!