本帖最后由 话痨司机啊 于 2022-7-8 09:16 编辑
Wallhaven 大名鼎鼎的壁纸下载网站,先说明一下出错看日志,如果是连接错误就是因为“墙”,请自行开代{过}{滤}理,本程序可以自动挂上你开的代{过}{滤}理端口。如果其他报错,请粘贴错误。
防止有人杠我,提前告知不是缩略图!!
【exe 双击运行 成品下载】:链接: https://pan.baidu.com/s/16BgiMLTdGJhl2HzxqyfeaQ?pwd=91eb 提取码: 91eb
效果图:
[Python] 纯文本查看 复制代码 """
_description_ : wallhaven 热门壁纸下载,日志在文件夹内
_ author_ : 话痨司机(俄语发音)
"""
import requests
from lxml import etree
from pathlib import Path
from rich import print
from loguru import logger
from urllib.request import getproxies
logger.add(Path(__file__).parent.joinpath('wallhaven_download.log'))
def get_local_proxy():
"""
尝试获取代{过}{滤}理
"""
global my_proxies
try:
my_proxies = getproxies()['http'] or getproxies()['https']
except KeyError:
my_proxies = None
def get_response(url):
'''
获取response
'''
try:
res = requests.get(url,timeout=30,proxies=my_proxies)
return res
except Exception as e:
logger.exception(e)
@logger.catch
def gets_the_primary_jpg(response):
'''
初步获取图片地址
'''
xpath_path = '//div[@id="thumbs"]/section[@class="thumb-listing-page"]/ul'
et = etree.HTML(response.text)
li = et.xpath(xpath_path)[0]
pic_datas = []
for l in li:
if l is not None:
pic_url = l.xpath('figure/img/@data-src')[0]
span = l.xpath('figure/div/span/span/text()')
if 'PNG' in span:
pic_datas.append((pic_url,'png'))
else:
pic_datas.append((pic_url,'jpg'))
return pic_datas
def address_translation(pic_data):
"""
处理初步获取的地址
"""
imgurl = pic_data[0].replace("//th.","//w.").replace("/small/","/full/")
img_url_list = imgurl.split('/')
img_url_list[-1] = 'wallhaven-' + img_url_list[-1]
imgurl ='/'.join(img_url_list)
if pic_data[1] == 'png':
imgurl = imgurl.replace('.jpg','.png')
file_name = imgurl.split('/')[-1]
return imgurl,file_name
def save_image(data):
"""
保存图片
"""
imgurl,file_name = data
image_dir = Path(__file__).parent.joinpath('wallhaven')
image_dir.mkdir(parents=True,exist_ok=True)
if not image_dir.joinpath(file_name).exists():
r = get_response(imgurl)
with open(image_dir.joinpath(file_name),'wb') as f:
f.write(r.content)
logger.info(f'图片:{file_name}\n保存路径:{image_dir}\n状态:下载完成')
else:
logger.warning(f'图片{file_name}已经下载,不能重复下载!')
def main(start_page,end_page):
'''
逻辑主函数
'''
_url = lambda num: f"https://wallhaven.cc/hot?page={num}"
urls = [_url(i) for i in range(start_page,end_page+1)]
for u in urls:
list(map(save_image,[data for data in map(address_translation,gets_the_primary_jpg(get_response(u)))]))
if __name__ == '__main__':
get_local_proxy()
start_page,end_page = int(input('请输入起始页面:')),int(input('请输入终止页面:'))
if start_page >= 1 and end_page <= 56:
main(start_page, end_page)
else:
print('[red]输入的页码超过限制,请重新输入,页码范围:1-56!')
|