Python新手练习多线程一个简单的例子,图片1280*800(非高清)
![](https://img.dpm.org.cn/Uploads/Picture/2023/07/30/s64c6545c12498.jpg)
![](https://img.dpm.org.cn/Uploads/Picture/2022/11/08/s6369f4aae4920.jpg)
代码如下:
[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')
![](https://s1.ax1x.com/2023/08/13/pPKF7ZQ.png)
|