python pywin32库 : Python 操作 windows 系统 API 【转】

导入数据
import requests import re
请求数据
for page in range(1, 126):
url = 'https://wallhaven.cc/toplist?page={}'.format(page)
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
解析数据
urls = re.findall('<a class="preview" href="(.*?)"', response.text)
for i in urls:
response_2 = requests.get(url=i, headers=headers)
img_url = re.findall('<img id="wallpaper" src="(.*?)"', response_2.text)[0]
title = img_url.split('-')[-1]
download(title, img_url)
print(img_url)
保存数据
def download(title, url):
path = 'img\\' + title
response = requests.get(url=url)
with open(path, mode='wb') as f:
f.write(response.content)
自动跟换桌面壁纸代码
import win32api
import win32con
import win32gui
import os
import time
def Windows_img(paperPath):
k=win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control panel\\Desktop",0,win32con.KEY_SET_VALUE)
# 在注册表中写入属性值
win32api.RegSetValueEx(k,"wapaperStyle",0,win32con.REG_SZ,"2") # 0 代表桌面居中 2 代表拉伸桌面
win32api.RegSetValueEx(k,"Tilewallpaper",0,win32con.REG_SZ,"0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,paperPath,win32con.SPIF_SENDWININICHANGE) # 刷新桌面
def changeWallpaper():
"""文件夹/文件夹/图片"""
path=input('请输入文件路径:')
L2=os.listdir(path=path) # 得到文件路径下的壁纸文件夹,列表类型
i=0
print(L2) # 壁纸文件夹
url_list = []
for l2 in L2:
detail_path = path + '\\' + l2
L3 = os.listdir(detail_path) # 得到壁纸文件夹路径下的图片,列表类型
for l3 in L3:
url_list.append(detail_path + '\\' + l3)
print(url_list)
while True:
Windows_img(url_list[i])
print('{}'.format(url_list[i]))
time.sleep(2) # 设置壁纸更换间隔,这里为10秒,根据用户自身需要自己设置秒数
i += 1
if i == len(url_list): # 如果是最后一张图片,则重新到第一张
i = 0
def changeWallpaper_2():
"""文件夹/图片"""
path=input('请输入文件路径:')
L2=os.listdir(path=path) # 得到文件路径下的图片,列表类型
i=0
print(L2)
while True:
Windows_img(path+'\{}'.format(L2[i]))
print(path+'\{}'.format(L2[i]))
time.sleep(1000) # 设置壁纸更换间隔,这里为10秒,根据用户自身需要自己设置秒数
i += 1
if i==len(L2): # 如果是最后一张图片,则重新到第一张
i=0
if __name__ == '__main__':
changeWallpaper()
最后实现效果

《pywin32库 : Python 操作 windows 系统 API》https://blog.csdn.net/freeking101/article/details/88231952

import requests import re import os import json import random import time from fake_useragent import UserAgent ua = UserAgent(path='./fake-useragent.json') # with open('fake-useragent.json','r',encoding='utf-8') as f: # user_agent_json = json.loads(f.read()) # useragent = random.choice(user_agent_json['browsers']['chrome']) # print(useragent) # 保存数据 def download(title, url): path = 'C:\\Users\\chenwandong01\\Pictures\\wallhaven\\' + title response_content = requests.get(url=url) time.sleep(10) with open(path, mode='wb') as f: f.write(response_content.content) # 请求数据 for page in range(1, 148): url = 'https://wallhaven.cc/toplist?page={}'.format(page) headers = {'user-agent': ua.random} response = requests.get(url=url, headers=headers) time.sleep(5) # 解析数据 urls = re.findall('<a class="preview" href="(.*?)"', response.text) for i in urls: response_2 = requests.get(url=i, headers=headers) time.sleep(5) img_url = re.findall('<img id="wallpaper" src="(.*?)"', response_2.text)[0] title = img_url.split('-')[-1] download(title, img_url) print(img_url,'下载完成')


浙公网安备 33010602011771号