吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1682|回复: 17
收起左侧

[Python 原创] Python多线程下载故宫壁纸

[复制链接]
surepj 发表于 2023-8-13 17:28
Python新手练习多线程一个简单的例子,图片1280*800(非高清)


代码如下:
[Python] 纯文本查看 复制代码
import time
import os
import requests
from lxml import etree
from multiprocessing.dummy import Pool

headers = {
    "Accept": "*/*",
    "Referer": "https://www.dpm.org.cn/lights/royal.html",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
}


def get_img_urls(url):
    response = requests.get(url, headers=headers)
    response.encoding = 'utf-8'
    # print(response.text)
    html = etree.HTML(response.text)
    imgs = html.xpath('//div[@class="pic"]/a/img')
    multi_pool = Pool(16)  # 创建16个线程池
    for i in imgs:
        title = i.xpath('./@title')[0]
        src = i.xpath('./@src')[0]
        multi_pool.apply_async(down_load, args=(title, src))
    multi_pool.close()
    multi_pool.join()


def down_load(name, url):
    res = requests.get(url, headers=headers)
    global count
    path = './故宫壁纸'  # 下载路径(当前文件夹下‘故宫壁纸’文件夹)
    if not os.path.exists(path):
        os.mkdir(path)
    print(f'正在下载>>> {count}_{name}.jpg   ...')
    with open(f'{path}/{count}_{name}.jpg','wb') as f:
        f.write(res.content)
        print(f'图_{count} 下载完成!')
        count += 1
        time.sleep(0.1)


if __name__ == '__main__':
    star_t = time.time()
    count = 1
    url = "https://www.dpm.org.cn/lights/royal/p/{}.html"
    for page in range(1, 11):  # 下载1-10页的图
        get_img_urls(url.format(page))

    print(f'All Done in {time.time()-star_t:.3f} seconds')



免费评分

参与人数 2吾爱币 +6 热心值 +2 收起 理由
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
qinni8513 + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

BLUE7777777 发表于 2023-8-14 21:12
surepj 发表于 2023-8-14 08:58
感谢提醒,也没有暴力获取网站数据,也只是简单的几页图片数据,应该没事吧。

Python多线程爬虫就是大量访问服务器数据,容易视为网络攻击,你在内lu实名上网,手机也实名,怎么都容易抓到,当然你访问几个图片没什么事情,一般不会引起警觉!
假如你的线程几十上百个,就会引起关注了。
eWVhaA 发表于 2023-8-13 18:03
andycjdx 发表于 2023-8-13 20:31
earlc 发表于 2023-8-13 21:16
eWVhaA 发表于 2023-8-13 18:03
好心提醒下……org.cn和gov.cn别瞎爬

哈哈,就是就是,找点个人站练手稳当点
头像被屏蔽
moruye 发表于 2023-8-13 21:36
提示: 作者被禁止或删除 内容自动屏蔽
ffrank 发表于 2023-8-13 22:13
感谢,学习学习
5584444 发表于 2023-8-13 22:40
eWVhaA 发表于 2023-8-13 18:03
好心提醒下……org.cn和gov.cn别瞎爬

这么吓人的吗
 楼主| surepj 发表于 2023-8-14 08:58
eWVhaA 发表于 2023-8-13 18:03
好心提醒下……org.cn和gov.cn别瞎爬

感谢提醒,也没有暴力获取网站数据,也只是简单的几页图片数据,应该没事吧。
nccdap 发表于 2023-8-14 09:29
学习使人进步,感谢
ppxj0 发表于 2023-8-14 09:36
eWVhaA 发表于 2023-8-13 18:03
好心提醒下……org.cn和gov.cn别瞎爬

爬了,它怎么能查出来是谁么
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-9-13 00:38

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表