欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Pyhton多線程采集圖片方式

 更新時(shí)間:2023年12月01日 09:08:05   作者:三省同學(xué)  
這篇文章主要介紹了Pyhton多線程采集圖片方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

前言

需要大量圖片做數(shù)據(jù)采集是,這個(gè)時(shí)候就需要用到python獲取圖片,本篇以python多線程獲取圖片為例。

一、查看頁(yè)面元素

查看頁(yè)面源代碼。

二、請(qǐng)求url查看

通過(guò)F12查看請(qǐng)求url。

tn: resultjson_com
logid: 12339447258259285711
ipn: rj
ct: 201326592
is: 
fp: result
fr: 
word: 動(dòng)漫圖片
queryWord: 動(dòng)漫圖片
cl: 2
lm: -1
ie: utf-8
oe: utf-8
adpicid: 
st: -1
z: 
ic: 
hd: 
latest: 
copyright: 
s: 
se: 
tab: 
width: 
height: 
face: 0
istype: 2
qc: 
nc: 1
expermode: 
nojc: 
isAsync: 
pn: 60
rn: 30
gsm: 3c
1669373933133: 

代碼實(shí)現(xiàn)

# -*- coding: utf-8 -*-
import os
import re
import time
from multiprocessing import Pool
import requests
from multiprocessing.dummy import Pool as ThreadPool  # 線程池


def get_image(keyword, page_num, save_dir):
    # 瀏覽器偽裝
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
    # 請(qǐng)求url
    url = 'https://image.baidu.com/search/acjson?'
    n = 0;
    pn = 1  # pn是從第幾張圖片開始
    page_num = page_num + 1;
    for m in range(1, page_num):
        # 請(qǐng)求參數(shù)
        param = {'tn': 'resultjson_com',
                 'ipn': 'rj',
                 'ct': 201326592,
                 'is': '',
                 'fp': 'result',
                 'queryWord': keyword,
                 'cl': 2,
                 'lm': -1,
                 'ie': 'utf-8',
                 'oe': 'utf-8',
                 'adpicid': '',
                 'st': -1,
                 'z': '',
                 'ic': '',
                 'hd': 1,
                 'latest': '',
                 'copyright': '',
                 'word': keyword,
                 's': '',
                 'se': '',
                 'tab': '',
                 'width': '',
                 'height': '',
                 'face': 0,
                 'istype': 2,
                 'qc': '',
                 'nc': '1',
                 'fr': '',
                 'expermode': '',
                 'force': '',
                 'cg': '',
                 'pn': pn,
                 'rn': '30',
                 'gsm': '1e',
                 '1669373933133': ' '
                 }
        request = requests.get(url=url, headers=header, params=param)
        if request.status_code == 200:
            print('success.')
        request.encoding = 'utf-8'
        html = request.text
        image_url_list = re.findall('"thumbURL":"(.*?)",', html, re.S)
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)

        for image_url in image_url_list:
            image_data = requests.get(url=image_url, headers=header).content
            # with open(os.path.join(save_dir, "{}_{:06d}.jpg".format("1", n)), 'wb') as fp:
            #     fp.write(image_data)
            pool.apply_async(download, args=(n, image_data, save_dir), error_callback=func.err_call_back)
            n = n + 1
        pn += 29

class Func(object):
    def __init__(self):
        # 利用匿名函數(shù)模擬一個(gè)不可序列化象
        # 更常見的錯(cuò)誤寫法是,在這里初始化一個(gè)數(shù)據(jù)庫(kù)的長(zhǎng)鏈接
        self.num = lambda: None

    def work(self, num=None):
        self.num = num
        return self.num

    @staticmethod
    def call_back(res):
        print('Hello,World! {res}')

    @staticmethod
    def err_call_back(err):
        print('出錯(cuò)啦:[{}]'.format(err))


def download(n, image_data, save_dir):
    # time.sleep(1)
    fp = open(os.path.join(save_dir, "{}_{:06d}.jpg".format("1", n)), 'wb')
    fp.write(image_data)
    fp.close()


if __name__ == '__main__':
    func = Func()
    keyword = '動(dòng)漫圖片'
    save_dir = keyword
    page_num = int(input("頁(yè)數(shù):"))
    # 線程池中線程數(shù)
    pool = Pool(10)
    # pool = ThreadPool(5)
    # i = 0;
    # while i < page_num:
    get_image(keyword, page_num, save_dir)
        # i = i + 1
    print('完成')

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論