爬取美女图片并保存本地和多线程部分

多线程

import requests
from concurrent.futures import ThreadPoolExecutor
from lxml import html
etree = html.etree




def get_list(url):
    etree = html.etree
    list_resp = requests.get(url)
    list_resp.encoding = "utf-8"
    tree = etree.HTML(list_resp.content)
    list_gg = tree.xpath('//div[@class="item-media entry"]//a')
    with ThreadPoolExecutor(10) as t:
        for gg in list_gg[2:16]:
            if gg.xpath('./@title')[0] != '': #标题不为空
                gg_title = gg.xpath('./@title')[0]
                gg_href = "https://www.mn52.com"+gg.xpath('./@href')[0]
                print(gg_href,gg_title)

                t.submit(get_download,gg_href,gg_title) #多线程
                # get_download(gg_href,gg_title)


def get_download(gg_href,gg_title):

    etree = html.etree
    png_resp = requests.get(gg_href)
    png_resp.encoding = 'utf-8'
    tree = etree.HTML(png_resp.content)
    png_urls = tree.xpath('//div[@id="originalpic"]//img/@src')

    number = 1
    with ThreadPoolExecutor(10) as t:
        for png_url in png_urls:

            png_url = "https:" + png_url
            png_name = gg_title + str(number)
            number =int(number)+1
            with open("美女图片/" + png_name+".png", mode="wb") as f:
                png_down = requests.get(png_url)
                f.write(png_down.content)  # 图片内容写入文件
            print(f"{png_name}下载成功",)


if __name__ == '__main__':
    get_list("https://www.mn52.com/xingganmeinv/")


posted @   17。  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· spring官宣接入deepseek,真的太香了~
点击右上角即可分享
微信分享提示