如何多线程下载图片,需要调用down函数

复制代码
import requests
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor

# url = 'https://www.umei.cc/meinvtupian/meinvxiezhen/'
def down(url):
    headers = {
        'authority': 'www.umei.cc',
        'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7',
        'accept-language': 'zh-CN,zh;q=0.9',
        'cache-control': 'max-age=0',
   
        'referer': 'https://www.umei.cc/bizhitupian/',
        'sec-ch-ua': '"Chromium";v="122", "Not(A:Brand";v="24", "Google Chrome";v="122"',
        'sec-ch-ua-mobile': '?0',
        'sec-ch-ua-platform': '"Windows"',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-user': '?1',
        'upgrade-insecure-requests': '1',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36',
    }

    response = requests.get(url, cookies=cookies, headers=headers)
    response.encoding="utf-8"

    soup =BeautifulSoup(response.text,'lxml')
    photos = soup.select("div.title a")

    for p in photos:
         p_url = p['href'].split("=")[-1]
         pnurl = "https://www.umei.cc/"+p_url
         p_name =p.text
         print(pnurl,p_name)
         res = requests.get(url=pnurl,cookies=cookies, headers=headers)
         res.encoding=("utf-8"),
         soup = BeautifulSoup(res.text, 'lxml')
         pp = soup.select("div.big-pic a img")[-1]
         pp_url=pp.get('src')
         res1 = requests.get(url=pp_url, cookies=cookies, headers=headers)
         open(f'img/{p_name}.jpg','wb').write(res1.content)

if __name__ == "__main__":
    with ThreadPoolExecutor(10) as t:
        for i in range(1,217):
            url = f'https://www.umei.cc/meinvtupian/rentiyishu/index_{i}.htm'
            t.submit(down,url)
相关推荐
吴佳浩10 小时前
Python入门指南(五) - 为什么选择 FastAPI?
后端·python·fastapi
寰天柚子10 小时前
Java并发编程中的线程安全问题与解决方案全解析
java·开发语言·python
2503_9284115611 小时前
项目中的一些问题(补充)
人工智能·python·tensorflow
superman超哥11 小时前
仓颉语言中锁的实现机制深度剖析与并发实践
c语言·开发语言·c++·python·仓颉
vv_Ⅸ11 小时前
打卡day42
python
Lvan的前端笔记12 小时前
python:深入理解 Python 的 `__name__ == “__main__“` 与双下划线(dunder)机制
开发语言·python
爱笑的眼睛1112 小时前
深入解析Matplotlib Axes API:构建复杂可视化架构的核心
java·人工智能·python·ai
爱埋珊瑚海~~12 小时前
基于MediaCrawler爬取热点视频
大数据·python
工程师丶佛爷12 小时前
从零到一MCP集成:让模型实现从“想法”到“实践”的跃迁
大数据·人工智能·python
2501_9216494913 小时前
免费获取股票历史行情与分时K线数据 API
开发语言·后端·python·金融·数据分析