本帖最后由 winsphinx 于 2023-5-14 07:40 编辑
哈,楼主,我之前也弄了一个下载netbian后作为壁纸的程序,发上来请指正。
稍有不同的是,我用了中图。(楼主每页的是小图,点击后打开的页面是中图,当然真正的大图是要付费的了)
[Python] 纯文本查看 复制代码 #!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import random
import tempfile
import requests
import win32api
import win32con
import win32gui
from lxml import etree
from PIL import Image
class Wallpaper:
def __init__(self):
self.image = os.path.join(tempfile.gettempdir(), "wallpaper.jpg")
def crawl(self):
base_url = "https://pic.netbian.com" # 基础页
page_url = base_url + "/4kmeinv" # 分栏页
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.162 Safari/537.36"
}
page = requests.get(url=page_url, headers=headers)
tree = etree.HTML(page.text)
max_page = tree.xpath('//div[@class="page"]/a[last() - 1]/text()')[0] # 获取分栏页最大页数
i = random.randint(1, int(max_page)) # 随机进入页面
if i == 1:
page_url = page_url
else:
page_url = page_url + "/index_" + str(i) + ".html"
page = requests.get(url=page_url, headers=headers) # 获取此页面元素列表
tree = etree.HTML(page.text)
max_img = len(tree.xpath('//*[@id="main"]/div[3]/ul/li'))
img = tree.xpath('//*[@id="main"]/div[3]/ul/li[' + str(random.randint(1, max_img)) + "]/a/@href")[0] # 随机获取图像元素
new_url = base_url + img # 跳转新图像页面
new_page = requests.get(url=new_url, headers=headers)
new_tree = etree.HTML(new_page.text)
new_img = new_tree.xpath('//*[@id="img"]/img/@src')[0] # 获取此图像地址
image_url = base_url + new_img
with open(self.image, "wb") as f:
f.write(requests.get(image_url).content)
return self
def setup(self):
keyex = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(keyex, "WallpaperStyle", 0, win32con.REG_SZ, "6")
win32api.RegSetValueEx(keyex, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, self.image, win32con.SPIF_SENDWININICHANGE)
"""
WallpaperStyle = 10 and TileWallpaper = 0 make walpaper filled
WallpaperStyle = 6 and TileWallpaper = 0 make walpaper fitted
WallpaperStyle = 2 and TileWallpaper = 0 make walpaper stretched
WallpaperStyle = 0 and TileWallpaper = 0 make walpaper centered
WallpaperStyle = 0 and TileWallpaper = 1 make walpaper tiled
"""
if __name__ == "__main__":
wallpaper = Wallpaper()
wallpaper.crawl().setup()
|