最近发现了一个不错的壁纸网站 彼岸桌面,如果每天从该网站爬取一张图片后设置为桌面壁纸,就挺不错的。
图片爬取:
将图片设置桌面
'''换壁纸'''
import win32gui, win32con, win32api
def setWallPaper(imagepath='download/cache_wallpaper.png'):
keyex = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(keyex, "WallpaperStyle", 0, win32con.REG_SZ, "0")
win32api.RegSetValueEx(keyex, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, imagepath, win32con.SPIF_SENDWININICHANGE)
"""
@ProjectName:pyExamples
@FileName : 彼岸图纸设置
@Description:
@Time :2023/7/25
@Author :redsun
@Author_email:[email protected]
"""
import os
import requests
from lxml import etree
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
}
'''检查文件夹是否存在'''
def checkDir(dirname):
if not os.path.exists(dirname):
os.mkdir(dirname)
'''爬取壁纸'''
def crawlWallpaper(cache_dir='download'):
checkDir(cache_dir)
# 彼岸桌面的壁纸爬取代码
url_base = 'http://www.netbian.com/'
classification = 'youxi'
classification_url = url_base + classification
html_data = requests.get(classification_url)
if html_data.ok:
html_data = etree.HTML(html_data.content)
wallpaper_urls = html_data.xpath('.//img')
wallpaper_url, wallpaper_title = [(url.xpath('./@src')[0], url.xpath('./@alt')[0] ) for url in wallpaper_urls if 'http' in url.xpath('./@src')[0] and url.xpath('./@alt') if ' ' in url.xpath('./@alt')[0]][0]
wallpaper_title = wallpaper_title.replace(' ', '_')
wallpaper_list = html_data.xpath('//div[@class="wrap clearfix"]/div[@id="main"]/div[@class="list"]/ul/li')
picture_url = wallpaper_list[2].xpath('.//@href')[0]
if 'http' not in picture_url:
picture_url = url_base + picture_url
picture_data = requests.get(picture_url)
if picture_data.ok:
picture_html = etree.HTML(picture_data.content)
wallpaper_url = picture_html.xpath('.//div[@class="pic"]/p/a/img/@src')[0]
wallpaper_title = picture_html.xpath('.//div[@class="pic"]/p/a/img/@title')[0]
wallpaper_title = wallpaper_title.replace(' ', '_')
else:
picture_data = requests.get(picture_url)
if picture_data.ok:
picture_html = etree.HTML(picture_data.content)
wallpaper_url = picture_html.xpath('.//div[@class="photo-pic"]/a/img/@src')[0]
wallpaper_url = url_base + wallpaper_url
wallpaper_title = picture_html.xpath('.//div[@class="photo-pic"]/a/img/@title')[0]
wallpaper_title = wallpaper_title.replace(' ', '_')
cur_picture_path = os.path.join(cache_dir, f'{wallpaper_title}.png')
with open(cur_picture_path, 'wb') as f:
res = requests.get(wallpaper_url, stream=True, verify=False)
for chunk in res.iter_content(chunk_size=1024): f.write(chunk)
return cur_picture_path
'''换壁纸'''
import win32gui, win32con, win32api
def setWallPaper(imagepath='download/cache_wallpaper.png'):
keyex = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(keyex, "WallpaperStyle", 0, win32con.REG_SZ, "0")
win32api.RegSetValueEx(keyex, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, imagepath, win32con.SPIF_SENDWININICHANGE)
'''主函数'''
def main():
image_path = crawlWallpaper('download')
setWallPaper(image_path)
'''run'''
if __name__ == '__main__':
main()