吾爱大佬分享的一个免费壁纸网站-轻壁纸,都是高清免费壁纸,附加一段Python自动爬取轻壁纸的脚本,0分钟换一张壁纸,都是4K 2K的壁纸。
Python爬取脚本
import requests from pathlib import Path from lxml import etree from rich import print from loguru import logger from requests.adapters import HTTPAdapter logpath = Path(__file__).parent.joinpath('img.log') logger.add(str(logpath)) def get_res(url): """ 获取网页内容 """ headers = { "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" } r = requests.Session() r.mount('https://',HTTPAdapter(max_retries=5)) res = r.get(url, headers=headers,timeout=30) return res def parse_src(res): """ 分析src,获取图片下载链接 """ try: et = etree.HTML(res.text) masonry = et.xpath("//div[@class='masonry']")[-1] src = masonry.xpath("//article//a[@class='entry-thumbnail']/img/@data-src") img_url_list = [] for s in src: img_url_list.append("-".join(s.split('x')[0].split('-')[:-1]) + "." + s.split('x')[1].split('.')[-1]) return img_url_list except Exception as e: logger.error(f"此页{res.url}访问失败,请重试!") def download_img(img_url_list): """ 下载图片 """ if type(img_url_list) is list: path = Path(__file__).parent.joinpath('images') path.mkdir(parents = True, exist_ok = True) file_name = [imgurl.split('/')[-1].replace("?","") for imgurl in img_url_list] for i,imgurl in enumerate(img_url_list): if path.joinpath(file_name[i]).exists(): img_url_list.remove(imgurl) print(f"文件{file_name[i]}已下载不能重复下载") if len(img_url_list)>0: ress = map(get_res, img_url_list) for i, res in enumerate(ress): if res: with open(str(path.joinpath(file_name[i])), 'wb') as f: f.write(res.content) print(f'已经成功下载{file_name[i]},保存在{str(path)}') def main(startnum=1,endnum=20): ''' 逻辑主函数 ''' url = lambda num: f"https://bz.qinggongju.com/page/{num}/" urls = [url(i) for i in range(startnum,endnum+1)] list(map(download_img, [image_url_list for image_url_list in map(parse_src, [res for res in map(get_res, urls)])])) if __name__ == "__main__": startnum = input('共20页热门图片,请输入开始页面数字:') endnum = input('请输入结束页面数字,不能超过20:') if int(startnum) >= 1 and int(endnum) <= 20 : main(int(startnum),int(endnum)) else: print('[red] Error:请重新启动程序输入数字!')