如何多线程下载图片,需要调用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)
相关推荐
u01092727117 分钟前
RESTful API设计最佳实践(Python版)
jvm·数据库·python
我材不敲代码4 小时前
Python实现打包贪吃蛇游戏
开发语言·python·游戏
0思必得06 小时前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
韩立学长6 小时前
【开题答辩实录分享】以《基于Python的大学超市仓储信息管理系统的设计与实现》为例进行选题答辩实录分享
开发语言·python
qq_192779876 小时前
高级爬虫技巧:处理JavaScript渲染(Selenium)
jvm·数据库·python
u0109272717 小时前
使用Plotly创建交互式图表
jvm·数据库·python
爱学习的阿磊7 小时前
Python GUI开发:Tkinter入门教程
jvm·数据库·python
Imm7777 小时前
中国知名的车膜品牌推荐几家
人工智能·python
tudficdew8 小时前
实战:用Python分析某电商销售数据
jvm·数据库·python
sjjhd6528 小时前
Python日志记录(Logging)最佳实践
jvm·数据库·python