如何多线程下载图片,需要调用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)
相关推荐
小王不爱笑1321 分钟前
Java基础知识(十四)
java·windows·python
烟锁池塘柳026 分钟前
【已解决,亲测有效】解决使用Python Matplotlib库绘制图表中出现中文乱码(中文显示为框)的问题的方法
开发语言·python·matplotlib
周小码28 分钟前
llama-stack实战:Python构建Llama应用的可组合开发框架(8k星)
开发语言·python·llama
IT学长编程33 分钟前
计算机毕业设计 基于Hadoop的南昌房价数据分析系统的设计与实现 Python 大数据毕业设计 Hadoop毕业设计选题【附源码+文档报告+安装调试
大数据·hadoop·python·毕业设计·课程设计·毕业论文·豆瓣电影数据可视化分析
郑洁文1 小时前
豆瓣网影视数据分析与应用
大数据·python·数据挖掘·数据分析
kyle~1 小时前
python---PyInstaller(将Python脚本打包为可执行文件)
开发语言·前端·python·qt
guidovans1 小时前
Crawl4AI精准提取结构化数据
人工智能·python·tensorflow
言之。2 小时前
Django REST Framework响应类Response详解
后端·python·django
Abadbeginning2 小时前
FastSoyAdmin centos7云服务器+宝塔部署
vue.js·后端·python
kida_yuan3 小时前
【从零开始】13. 数据增强(Data Augmentation)
数据结构·python·nlp