Awesome Wallpapers是我比较喜欢的壁纸网站,里面壁纸都十分精美。
我这个人又喜欢搞整合,于是就用爬虫写了个下载壁纸工具
因为是新手所以做的不是很好,就当做第一次写文章的纪念了。
import requests
from lxml import etree
import os
def download_picture(page,categories,topRange):
url = "https://wallhaven.cc/search?"
parmas = {
"categories": categories,
"purity": "100",
"topRange": topRange,
"sorting": "toplist",
"order": "desc",
"page": page
}
headers = {
"user-agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.56'
}
proxies = {
}
response = requests.get(url=url,params=parmas,headers=headers).text
tree = etree.HTML(response)
pictureList = tree.xpath("//a[@class='preview']")
print("网页读取成功!开始下载壁纸!")
i=0
for picture in pictureList:
picture_src = picture.xpath('@href')[0]
picture_web = requests.get(url=picture_src,headers=headers).text
newtree = etree.HTML(picture_web)
picture_src1 = newtree.xpath("//img[@id='wallpaper']/@src")[0]
picture_data = requests.get(url=picture_src1,headers=headers).content
picture_name = "类型_"+categories+" 第"+str(page)+"页 第"+str(i+1)+"张图片"+".jpg"
i+=1
isExists = os.path.exists("pictures") #判断是否存在pictures目录,如果没有则创建
if not isExists:
os.mkdir("pictures")
picture_path = "./pictures/"+picture_name
with open(picture_path,"wb") as fp:
fp.write(picture_data)
print(picture_src + "下载完成 大小为:"+str(round(os.path.getsize(picture_path)/1024/1024))+"M")
if __name__ == "__main__":
pagetotal = input("请输入你要下载的页数\n")
categories = input("选择你要下载的类型:一、下载其他类型输入100 二、下载动漫类型输入010 三、下载真人壁纸输入001 \n也可以选择多种类型\n")
topRange = input("选择日期 1.一天1d 2.三天3d 3.一周1w 4.一个月1M 5.三个月3M 6.半年6M 7.一年1y\n")
page=1
while True:
if page<=int(pagetotal):
download_picture(page,categories,topRange)
page+=1
else : break
print("下载完成,请在程序根目录查看!")
为了方便使用,下面是我打包好的exe文件简易壁纸下载器: 很简单对吧 (gitee.com)
下载可执行文件即可