一张图片看久了有些审美疲劳,于是决定换个老婆\手动狗头,不是,壁纸
思路:
- 1.获取图片,这个需要编写一段爬虫,去某些网站获取高清图,我选的是wallpaper,别的网站后续会加上,为了加速,我开了多线程,由于就下载少量图片,所以不考虑对方网络的承受压力问题
- 2.获取的图片覆盖之前的图片,这个只需要文件名覆盖写入即可
- 3.定时启动、或者开机启动,win自带
- 4.设置壁纸幻灯片播放,win自带
- 5.升级,获取更多网站的图片(这里提一下:wallpaper有个ramdon,专门随机的,但是我没有直接爬取这个网站,因为预想爬指定标签内容~)
import re
import requests
import time
from lxml import etree
import random
import threading
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
}
def get_img_url_from_wallpaper(index, time=0):
base_url = "https://wallpaper.wispx.cn/detail/{}"
try:
url = base_url.format(index)
rep = requests.get(url, headers=headers)
html = etree.HTML(rep.text)
# print(rep.text)
imgurl = html.xpath(
"//a[@class='mdui-ripple mdui-ripple-white']/@href")[0]
return imgurl
except:
print("爬取错误")
time = time+1
if time < 5:
get_img_url_from_wallpaper(random.randint(2, 883), time)
def down_img_as_wallpaper(imgurl, i):
rep = requests.get(imgurl, headers=headers)
# print(imgurl)
if rep.status_code == 200:
with open('C:/Users/YBJB/Pictures/DesktopImages/wallpaper{}.jpg'.format(i), 'wb') as f:
f.write(rep.content)
def multi_thread_wallpaper():
t = []
for i in range(10):
img_index = random.randint(2, 883)
t.append(threading.Thread(target=down_img_as_wallpaper, name="{}".format(
i), args=(get_img_url_from_wallpaper(img_index), i)))
t[i].start()
print("thread{}:start".format(i))
if __name__ == '__main__':
# base_urls = ['https://wallpaper.wispx.cn/detail/{}',
# 'http://www.obzhi.com/{}.html']
# randrange = [[2, 883], [30, 3000]]
multi_thread_wallpaper()
# multi_thread_obzhi("http://www.obzhi.com/{}.html")
print("完成")
- 编写完毕之后,将其放在开机启动文件夹中,这样每次开机都会运行一遍这个爬虫
- 接下来指定文件夹,右键个性化,背景改为幻灯片放映
- 点击浏览,选择我们下载图片的文件夹
- 其他就是图片设置,我们设置好了就算大功告成了。
最终效果
补充
- 如果,想要效果明显一点,那么就可以吧壁纸切换时间设置的小一点,加大获取的图片数量,同时关闭我们的原有的壁纸插件什么的,那玩意可能会改变背景设置,最后给一张今天的桌面图~运气还不错,这张我还挺喜欢哈哈
补充2:请求头修改
- 跑了几天,发现发现不再下载了,应当是机制改变了,捣鼓了一会发现需要再header中加入referer元素才可以下载,而referer保存这detail的URL,换句话说想下载图片,则必须经过detail页面,否则侥幸蒙对了下载地址也是不对的,于是最终代码是:
最终代码
import re
import requests
import time
from lxml import etree
import random
import threading
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36"
}
def get_img_url_from_wallpaper_random( time=0):
base_url = "https://wallpaper.wispx.cn/random"
try:
rep = requests.get(base_url, headers=headers)
html = etree.HTML(rep.text)
print(rep.text)
imgurl = html.xpath(
"//img/@src")[0]
return imgurl
except:
print("爬取错误")
time = time+1
if time < 5:
get_img_url_from_wallpaper_random(time)
def get_img_url_from_wallpaper_by_index(index, time=0):
base_url = "https://wallpaper.wispx.cn/detail/{}"
try:
url = base_url.format(index)
rep = requests.get(url, headers=headers)
html = etree.HTML(rep.text)
# print(rep.text)
imgurl = html.xpath(
"//a[@class='mdui-ripple mdui-ripple-white']/@href")[0]
return imgurl , url
except:
print("爬取错误")
time = time+1
if time < 5:
get_img_url_from_wallpaper_by_index(random.randint(2, 883), time)
def down_img_as_wallpaper(imgurl , url , i):
header = headers
header["referer"] = url
rep = requests.get(imgurl, headers=header)
print(rep.status_code)
if rep.status_code == 200:
with open('D:/DesktopImages/wallpaper{}.jpg'.format(i), 'wb') as f:
f.write(rep.content)
def multi_thread_wallpaper():
t = []
for i in range(10):
img_index = random.randint(2, 883)
print(img_index)
img_url , referer_url = get_img_url_from_wallpaper_by_index(img_index)
t.append(threading.Thread(target=down_img_as_wallpaper, name="{}".format(
i), args=(img_url , referer_url, i)))
# t.append(threading.Thread(target=down_img_as_wallpaper, name="{}".format(
# i), args=(get_img_url_from_wallpaper_random(), i)))
t[i].start()
print("thread{}:start".format(i))
if __name__ == '__main__':
# base_urls = ['https://wallpaper.wispx.cn/detail/{}',
# 'http://www.obzhi.com/{}.html']
# randrange = [[2, 883], [30, 3000]]
multi_thread_wallpaper()
# multi_thread_obzhi("http://www.obzhi.com/{}.html")
print("完成")
补充3:打包+全部源码(有个exe可在没有python的x64电脑上用)
转载:https://blog.csdn.net/symuamua/article/details/105002360
查看评论