1.爬取图片,作为壁纸,用来接下来的壁纸自动切换
python代码里的库如果报错,记得下库
pip install 加库名
代码
import requests
import parsel
# 10,15爬取10到15页的图片
for pag in range(10, 15):
print(f'=========={pag}页=====')
# 设置网页
url = f'http://www.netbian.com/meinv/index_{pag}.htm'
# 设置浏览器
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
#
responrse = requests.get(url=url, headers=headers)
# html_data = responrse.content.decode('utf-8')
responrse.encoding = responrse.apparent_encoding
# print(responrse.text)
selctor = parsel.Selector(responrse.text)
lis = selctor.css('.list li')
# print(lis)
for li in lis:
title = li.css('b::text').get()
# print(title)
if title:
href = 'http://www.netbian.com/' + li.css('a::attr(href)').get()
# print(href, title)
response_i = requests.get(url=href, headers=headers)
seletor_i = parsel.Selector(response_i.text)
img_url = seletor_i.css('.pic img::attr(src)').get()
# print(href,title,img_url)
img_countent = requests.get(url=img_url, headers=headers).content
# 图片保存在当前img目录下
with open('img\\' + title + '.jpg', mode='wb') as f:
f.write(img_countent)
print('正在保存:' + title)
设置浏览器
运行代码,图片保存在img目录下
2.编写自动切换壁纸代码
import random
import ctypes
import time
import datetime
import os
from xml.dom.minidom import parse
picture_list = []
path = ""
random_time = 68
def read_xml():
doc = parse("./config.xml")
root = doc.documentElement
global path,random_time
path = root.getElementsByTagName("path")[0].firstChild.data
random_time = int(root.getElementsByTagName("random_time")[0].firstChild.data)
def list_all_pic(pic_dir):
pic_files = []
files_list =os.listdir(pic_dir)
for i in range(0,len(files_list)):
full_path = os.path.join(pic_dir,files_list[i])
if os.path.isfile(full_path):
if full_path.rsplit(".")[-1].lower() in ["jpg","bmp","jpeg","png"]:
pic_files.append(full_path)
return pic_files
def random_pic_index():
return picture_list[random.randint(0,len(picture_list)-1)]
def main():
read_xml()
global picture_list
picture_list = list_all_pic(path)
if len(picture_list) == 0:
print("文件夹中没有壁纸")
exit(1)
while True:
file_name = random_pic_index()
ctypes.windll.user32.SystemParametersInfoW(20,0,file_name,0)
print("{} 图像名:{}".format(datetime.datetime.strftime(datetime.datetime.now(),"%y-%m-%d %H:%M:%S"),file_name))
time.sleep(random_time)
if __name__ == '__main__':
main()
```python
编写.xml文件
<?xml version="1.0"?>
<config>
<!-- 保存图片目录-->
<path>F:/python/pythonFile/wallpaper/img</path>
<!-- 图片切换时间-->
<random_time>60</random_time>
</config>
运行桌面查看效果
**
3.打包成exe文件
**
1.win+R
cmd 打开命令行窗口
首先安装pyinstaller,使用安装命令:pip3 install pyinstaller
2.找到python文件夹运行cmd回车打开命令行
运行命令 pyinstaller -F 文件名.py
成功后 项目dist目录下有exe程序,把他移到.py文件同一目录
双击运行
3.添加到系统自启文件夹,实现开机自启
1.创建快捷方式
2.命令行打开自启文件夹(shell:startup),放入快捷方式
壁纸实现开机自启自动切换