#导入多线程模块:
import threading
import os
import requests # 发送请求
import time
from bs4 import BeautifulSoup # 解析文本
#导入线程池执行器和进程池执行器:
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
#导入获取CPU的数量cpu_count模块的功能:
from multiprocessing import cpu_count
base_path = os.path.dirname(os.path.abspath(__file__))
img_path = os.path.join(base_path, 'img')
def func(num):
#小f字符串拼接下:
response = requests.get(f'http://pic.yesky.com/c/6_20491_{num}.shtml')
soup = BeautifulSoup(response.text, 'html.parser') # 将请求结果交给bs4解析
div_obj = soup.find(name='div', attrs={"class": "lb_box"}) # 经过分析之后,定位到指定div
list_dd = div_obj.find_all(name='dd')
for dd in list_dd: # 每一张图片的dl
a_obj = dd.find('a')
# 拼接文件夹的路径,并创建文件夹
dir_path = os.path.join(img_path, a_obj.text)
if not os.path.isdir(dir_path): # 判断文件是否存在
os.mkdir(dir_path)
a_response = requests.get(a_obj.get('href'))
a_response.encoding = 'GBK'
soup2 = BeautifulSoup(a_response.text, 'html.parser')
div_obj2 = soup2.find(name='div', attrs={"class": "overview"})
# print(div_obj2)
print(response.url)
try:
img_list = div_obj2.find_all(name='img')
for img in img_list:
img_src = img.get("src")
img_response = requests.get(img_src.replace('113x113', '740x-'))
file_path = os.path.join(dir_path, img_src.rsplit('/', 1)[-1])
with open(file_path, 'wb') as f:
f.write(img_response.content)
except Exception as e:
pass
if __name__ == '__main__':
#获取电脑CPU的数量:
# print(cpu_count())
#定义开始时间:
start = time.time()
#开当前电脑的cup核数的进程池:
# p = ProcessPoolExecutor(max_workers=cpu_count())
#循环5圈:
# for i in range(1,6):
#获取进程池并submit提交下:
# p.submit(func,i)
# p.shutdown()
#开当前电脑的cup核数的线程池:
t = ThreadPoolExecutor(max_workers=cpu_count())
for i in range(1,6):
t.submit(func,i)
t.shutdown()
print("执行时间:{}".format(time.time() - start))
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 趁着过年的时候手搓了一个低代码框架
· 用 C# 插值字符串处理器写一个 sscanf
· 推荐一个DeepSeek 大模型的免费 API 项目!兼容OpenAI接口!