话不多少,由于工作比较忙,以下直接源码分享,感兴趣的朋友可以留言交流
此原创源码,只限于测试学习使用。
"""导入相关模块"""import requestsfrom bs4 import BeautifulSoupimport timeimport os"""确定一级网页URL"""
url_1 = 'http://www.win4000.com/wallpaper_205_0_0_1.html'
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36'
headers = {'User-Agent': user_agent}
res_1 = requests.get(url_1, headers=headers)if res_1.status_code == requests.codes.ok:
print("---网页请求成功,开始网页分析---")
soup_1 = BeautifulSoup(res_1.text, 'lxml')
time.sleep(0.4)if soup_1 != None:
print("---网页分析成功,开始数据提取---")
time.sleep(0.4)
style_contents = soup_1.find('div', class_="list_cont Left_list_cont Left_list_cont")
url_2 = style_contents.find('a')['href']"""循环传递二级网页URL"""
flag = 1
name = ''
sum = 0while flag:
res_2 = requests.get(url_2, headers=headers)
soup_2 = BeautifulSoup(res_2.text, 'lxml')
picture_info = soup_2.find('img', class_="pic-large")
picture_url = picture_info['src']
picture_type_name = picture_info['title']
picture = requests.get(picture_url, headers=headers) if name != picture_type_name:
i = 1
os.makedirs(f'C:/Users/三悟小道/Pictures/WIN4000桌面壁纸/大陆明星壁纸/{picture_type_name}')
print(f"文件夹--{picture_type_name}创建完毕") with open(f'C:/Users/三悟小道/Pictures/WIN4000桌面壁纸/大陆明星壁纸/{picture_type_name}/'+picture_type_name+str(i)+'.jpg', 'wb') as ps:
ps.write(picture.content)
ps.close()
sum += 1
time.sleep(0.45)
name = picture_type_name
picture_next = soup_2.find('div', class_="pic-next-img")
picture_next_url = picture_next.find('a')['href']
print(f"{picture_type_name}{i}.jpg下载完毕")
url_2 = picture_next_url
flag += 1
i += 1
print(f"---总共下载了{sum}张图片---")