欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Python如何多線程并發(fā)下載圖片

本文小編為大家詳細(xì)介紹“Python如何多線程并發(fā)下載圖片”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Python如何多線程并發(fā)下載圖片”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),臨猗企業(yè)網(wǎng)站建設(shè),臨猗品牌網(wǎng)站建設(shè),網(wǎng)站定制,臨猗網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,臨猗網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

最終效果

這就是我們最終要構(gòu)建的效果。

Python如何多線程并發(fā)下載圖片

Python如何多線程并發(fā)下載圖片

安裝依賴項(xiàng)

讓我們安裝每個(gè)人最喜歡的 requests 庫(kù)。

pip install requests

現(xiàn)在,我們將看到一些用于下載單個(gè) URL 并嘗試自動(dòng)查找圖像名稱以及如何使用重試的基本代碼。

import requests

res = requests.get(img_url, stream=True)
count = 1
while res.status_code != 200 and count <= 5:
    res = requests.get(img_url, stream=True)
    print(f'Retry: {count} {img_url}')
    count += 1

在這里,我們重試下載圖像五次,以防失敗。現(xiàn)在,讓我們嘗試自動(dòng)找到圖像的名稱并保存它。

import more required library

import io
from PIL import Image

# lets try to find the image name
image_name = str(img_url[(img_url.rfind('/')) + 1:])
if '?' in image_name:
    image_name = image_name[:image_name.find('?')]

解釋

假設(shè)我們要下載的 URL 是:

instagram.fktm7-1.fna.fbcdn.net/vp...

好吧,這是一團(tuán)糟。讓我們分解一下代碼對(duì) URL 的作用。我們首先使用 rfind 找到最后一個(gè)正斜杠(/),然后選擇之后的所有內(nèi)容。這是結(jié)果:

65872070_1200425330158967_6201268309743367902_n.jpg

現(xiàn)在我們的第二部分找到一個(gè) ?,然后只取它前面的任何東西。

這是我們最終的圖像名稱:

65872070_1200425330158967_6201268309743367902_n.jpg

這個(gè)結(jié)果非常好,適用于大多數(shù)用例。

現(xiàn)在我們已經(jīng)下載了圖像名稱和圖像,我們將保存它。

i = Image.open(io.BytesIO(res.content))
i.save(image_name)

如果你在想,「我到底應(yīng)該怎么使用上面的代碼?」那么你的想法是正確的。這是一個(gè)漂亮的函數(shù),我們?cè)谏厦嫠龅囊磺卸急槐馄教幚砹恕T谶@里,我們還測(cè)試了下載的類型是否為圖像,以防找不到圖像名稱。

def image_downloader(img_url: str):
    """
    Input:
    param: img_url  str (Image url)
    Tries to download the image url and use name provided in headers. Else it randomly picks a name
    """
    print(f'Downloading: {img_url}')
    res = requests.get(img_url, stream=True)
    count = 1
    while res.status_code != 200 and count <= 5:
        res = requests.get(img_url, stream=True)
        print(f'Retry: {count} {img_url}')
        count += 1
    # checking the type for image
    if 'image' not in res.headers.get("content-type", ''):
        print('ERROR: URL doesnot appear to be an image')
        return False
    # Trying to red image name from response headers
    try:
        image_name = str(img_url[(img_url.rfind('/')) + 1:])
        if '?' in image_name:
            image_name = image_name[:image_name.find('?')]
    except:
        image_name = str(random.randint(11111, 99999))+'.jpg'

    i = Image.open(io.BytesIO(res.content))
    download_location = 'cats'
    i.save(download_location + '/'+image_name)
    return f'Download complete: {img_url}'

現(xiàn)在,你可能會(huì)問:「這個(gè)人所說(shuō)的多處理在哪里?」。

這很簡(jiǎn)單。我們將簡(jiǎn)單地定義我們的池并將我們的函數(shù)和圖像 URL 傳遞給它。

results = ThreadPool(process).imap_unordered(image_downloader, images_url)
for r in results:
    print(r)

讓我們把它放在一個(gè)函數(shù)中:

def run_downloader(process:int, images_url:list):
    """
    Inputs:
        process: (int) number of process to run
        images_url:(list) list of images url
    """
    print(f'MESSAGE: Running {process} process')
    results = ThreadPool(process).imap_unordered(image_downloader, images_url)
    for r in results:
        print(r)

再一次,你可能會(huì)說(shuō),「這一切都很好,但我想立即開始下載我的 1000 張圖像列表。我不想復(fù)制和粘貼所有這些代碼并試圖弄清楚如何合并所有內(nèi)容。」

這是一個(gè)完整的腳本。它執(zhí)行以下操作:

  • 以圖像列表文本文件和進(jìn)程號(hào)作為輸入

  • 按照您想要的速度下載它們

  • 打印下載文件的總時(shí)間

  • 還有一些不錯(cuò)的函數(shù)可以幫助我們讀取文件名并處理錯(cuò)誤和其他東西

完整的腳本

# -*- coding: utf-8 -*-
import io
import random
import shutil
import sys
from multiprocessing.pool import ThreadPool
import pathlib

import requests
from PIL import Image
import time

start = time.time()

def get_download_location():
    try:
        url_input = sys.argv[1]
    except IndexError:
        print('ERROR: Please provide the txt file\n$python image_downloader.py cats.txt')
    name = url_input.split('.')[0]
    pathlib.Path(name).mkdir(parents=True, exist_ok=True)
    return name

def get_urls():
    """
    通過(guò)讀取終端中作為參數(shù)提供的 txt 文件返回 url 列表
    """
    try:
        url_input = sys.argv[1]
    except IndexError:
        print('ERROR: Please provide the txt file\n Example \n\n$python image_downloader.py dogs.txt \n\n')
        sys.exit()
    with open(url_input, 'r') as f:
        images_url = f.read().splitlines()

    print('{} Images detected'.format(len(images_url)))
    return images_url

def image_downloader(img_url: str):
    """
    輸入選項(xiàng):
    參數(shù): img_url  str (Image url)
    嘗試下載圖像 url 并使用標(biāo)題中提供的名稱。否則它會(huì)隨機(jī)選擇一個(gè)名字
    """
    print(f'Downloading: {img_url}')
    res = requests.get(img_url, stream=True)
    count = 1
    while res.status_code != 200 and count <= 5:
        res = requests.get(img_url, stream=True)
        print(f'Retry: {count} {img_url}')
        count += 1
    # checking the type for image
    if 'image' not in res.headers.get("content-type", ''):
        print('ERROR: URL doesnot appear to be an image')
        return False
    # Trying to red image name from response headers
    try:
        image_name = str(img_url[(img_url.rfind('/')) + 1:])
        if '?' in image_name:
            image_name = image_name[:image_name.find('?')]
    except:
        image_name = str(random.randint(11111, 99999))+'.jpg'

    i = Image.open(io.BytesIO(res.content))
    download_location = get_download_location()
    i.save(download_location + '/'+image_name)
    return f'Download complete: {img_url}'

def run_downloader(process:int, images_url:list):
    """
    輸入項(xiàng):
        process: (int) number of process to run
        images_url:(list) list of images url
    """
    print(f'MESSAGE: Running {process} process')
    results = ThreadPool(process).imap_unordered(image_downloader, images_url)
    for r in results:
        print(r)

try:
    num_process = int(sys.argv[2])
except:
    num_process = 10

images_url = get_urls()
run_downloader(num_process, images_url)

end = time.time()
print('Time taken to download {}'.format(len(get_urls())))
print(end - start)

將其保存到 Python 文件中,然后運(yùn)行它。

python3 image_downloader.py cats.txt

這是 GitHub 存儲(chǔ)庫(kù)的鏈接。

用法

python3 image_downloader.py <filename_with_urls_seperated_by_newline.txt> <num_of_process>

這將讀取文本文件中的所有 URL,并將它們下載到名稱與文件名相同的文件夾中。

num_of_process 是可選的(默認(rèn)情況下,它使用 10 個(gè)進(jìn)程)。

例子

python3 image_downloader.py cats.txt

Python如何多線程并發(fā)下載圖片

Python如何多線程并發(fā)下載圖片

讀到這里,這篇“Python如何多線程并發(fā)下載圖片”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

新聞標(biāo)題:Python如何多線程并發(fā)下載圖片
URL鏈接:http://www.chinadenli.net/article14/gccsge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)電子商務(wù)商城網(wǎng)站ChatGPT定制開發(fā)企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作