python多线程下载网页图片并保存至特定目录

Posted 无边身尊者

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python多线程下载网页图片并保存至特定目录相关的知识,希望对你有一定的参考价值。

#!python3
#multidownloadXkcd.py  - Download XKCD comics using multiple threads.

import requests
import bs4
import os
import threading

# os.mkdir(‘xkcd‘, exist_ok=True)     # store comics in ./xkcd
if os.path.exists(‘xkcd‘):
    print("xkcd is existed!")
else:
    os.mkdir(‘xkcd‘)

def downloadXkcd(startComic, endComic):
    for urlNumber in range(startComic, endComic):
        #Download the page
        print("Downloading page http://xkcd.com/%s..." % urlNumber)
        res = requests.get(‘http://xkcd.com/%s‘ % urlNumber)
        res.raise_for_status()

        print(res.text)
        soup = bs4.BeautifulSoup(res.text)

        #Find the URL of the comic image.
        comicElem = soup.select(‘#comic img‘)
        if comicElem == []:
            print(‘Could not find comic images.‘)
        else:
            comicUrl = comicElem[0].get(‘src‘)
        #     #Download the image.
        #     print(‘Downloading image %s...‘ % (comicUrl))
        #     res = requests.get(comicUrl)
        #     res.raise_for_status()
        #
        #     # Save the image to ./xkcd
        #     imageFile = open(os.path.join(‘xkcd‘, os.path.basename(comicUrl)), ‘wb‘)
        #     for chunk in res.iter_content(100000):
        #         imageFile.write(chunk)
        #     imageFile.close()

downloadThread = threading.Thread(target=downloadXkcd(555, 557))
downloadThread.start()

# # TODO: Create and start the thread objects
# downloadThreads = []        # a list of all the Thread objects
# for i in range(500, 600, 10):
#     downloadThread = threading.Thread(target=downloadXkcd, args=(i, i+9))
#     downloadThreads.append(downloadThread)
#     downloadThread.start()
#
# # TODO: Wait for all threads to end
# for downloadThread in downloadThreads:
#     downloadThread.join()
# print("Done.")

  

以上是关于python多线程下载网页图片并保存至特定目录的主要内容,如果未能解决你的问题,请参考以下文章

Python爬虫获取图片并下载保存至本地的实例

生活这么无聊,保存点小姐姐图片作为调料吧(多线程版本)

多线程高容错爬头条街拍美图

Python之多线程爬虫抓取网页图片

Java多线程断点下载文件并压缩

如何下载整个网站的图片(只能通过网址访问,无超链接)