最近学习了一些Python基础知识,想着利用Python做一些小的脚本,然后看到一篇博客,介绍了如何抓取Bing主页上的图片来作为电脑的壁纸,于是就照例实现一下,在实现的过程中遇到一些小问题,记录一下:
主要源于以下两篇文章做参考:
http://www.php.cn/python-tutorials-370699.html
https://blog.csdn.net/ZM_Yang/article/details/77801679
主要的实现Python代码:
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
from urllib.request import urlopen
from bs4 import BeautifulSoup
from PIL import Image
import win32gui
import win32con
import win32api
import re
import sys
import os
# Author : ZMYCHOU
# Version :V1.0
# Since :2017/09/02
# Modified:2017/09/02
# To run this program, you need follow module:
# Python 3.6
# win32 module,website :https://sourceforge.net/projects/pywin32/
# Image module,website :https://pypi.python.org/pypi/Pillow
# BeautifulSoup,website:https://www.crummy.com/software/BeautifulSoup/
# Navigate to the directory where this source file is located from DOS command and then enter
# follow command:
# python LiveWallPaper.py
def setWallpaper(imagepath):
k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2")
win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)
def setWallPaperBMP(imagePath):
bmpImage = Image.open(imagePath)
newPath = imagePath.replace('.jpg', '.bmp')
bmpImage.save(newPath, "BMP")
setWallpaper(newPath)
# Get image from Bing
bing = urlopen("http://cn.bing.com/").read()
str = bing.decode("gbk",'ignore')
imgUrl = re.search(r'az/hprichbg/rb(.){1,100}.jpg?', str)
if imgUrl:
print(imgUrl.group())
imgFile = open(os.getcwd() + r"\img.bmp", "wb+")
img = urlopen("http://cn.bing.com/" + imgUrl.group()).read()
imgFile.write(img)
imgFile.flush()
imgFile.close()
setWallPaperBMP(os.getcwd() + r"\img.bmp")
运行前,我们需要安装beautifulsoup4和PIL支持库
另外在运行过程中,会出现各种模块缺失无法运行,这个时候对应运行的批处理如下所示:
Microsoft Windows [版本 10.0.14393] (c) 2016 Microsoft Corporation。保留所有权利。 C:\Users\admin>pip3 Usage: pip3
[options] Commands: install Install packages. download Download packages. uninstall Uninstall packages. freeze Output installed packages in requirements format. list List installed packages. show Show information about installed packages. check Verify installed packages have compatible dependencies. config Manage local and global configuration. search Search PyPI for packages. wheel Build wheels from your requirements. hash Compute hashes of package archives. completion A helper command used for command completion. help Show help for commands. General Options: -h, --help Show help. --isolated Run pip in an isolated mode, ignoring environment variables and user configuration. -v, --verbose Give more output. Option is additive, and can be used up to 3 times. -V, --version Show version and exit. -q, --quiet Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels). --log Path to a verbose appending log. --proxy Specify a proxy in the form [user:passwd@]proxy.server:port. --retries Maximum number of retries each connection should attempt (default 5 times). --timeout Set the socket timeout (default 15 seconds). --exists-action Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort). --trusted-host Mark this host as trusted, even though it does not have valid or any HTTPS. --cert Path to alternate CA bundle. --client-cert Path to SSL client certificate, a single file containing the private key and the certificate in PEM format. --cache-dir Store the cache data in
. --no-cache-dir Disable the cache. --disable-pip-version-check Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index. --no-color Suppress colored output C:\Users\admin>python -m pip install --upgrade pip Collecting pip Downloading https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB) 100% |████████████████████████████████| 1.3MB 5.1MB/s Installing collected packages: pip Found existing installation: pip 10.0.1 Uninstalling pip-10.0.1: Successfully uninstalled pip-10.0.1 Successfully installed pip-18.0 C:\Users\admin>pip3 -V pip 18.0 from d:\python\python3.7.0\lib\site-packages\pip (python 3.7) C:\Users\admin>pip3 install beautifulsoup4 Collecting beautifulsoup4 Downloading https://files.pythonhosted.org/packages/21/0a/47fdf541c97fd9b6a610cb5fd518175308a7cc60569962e776ac52420387/beautifulsoup4-4.6.3-py3-none-any.whl (90kB) 100% |████████████████████████████████| 92kB 232kB/s Installing collected packages: beautifulsoup4 Successfully installed beautifulsoup4-4.6.3 C:\Users\admin>pip install pillow Collecting pillow Downloading https://files.pythonhosted.org/packages/39/a5/4258f77ab40d71bf7d724002a0bb166a902cb4cbdff7b866b1c3d9014547/Pillow-5.2.0-cp37-cp37m-win_amd64.whl (1.6MB) 100% |████████████████████████████████| 1.6MB 766kB/s Installing collected packages: pillow Successfully installed pillow-5.2.0 C:\Users\admin>pip install pypiwin32 Collecting pypiwin32 Downloading https://files.pythonhosted.org/packages/d0/1b/2f292bbd742e369a100c91faa0483172cd91a1a422a6692055ac920946c5/pypiwin32-223-py3-none-any.whl Collecting pywin32>=223 (from pypiwin32) Downloading https://files.pythonhosted.org/packages/f4/fb/ba9e519d90b63090005bcdee900d318d895d475c627dc1bc0f77b27abad0/pywin32-223-cp37-cp37m-win_amd64.whl (8.9MB) 100% |████████████████████████████████| 8.9MB 1.2MB/s Installing collected packages: pywin32, pypiwin32 Successfully installed pypiwin32-223 pywin32-223
接下来,我们运行Python脚本就可以抓取Bing主页图片作为我们的桌面壁纸了。
由于公司电脑默认在开机启动时会篡改我们预先设置好的桌面背景,这时候就希望写一个更换壁纸的python脚本,在每次开机时自动运行,实现如下:
1.先写一个设置桌面背景的python脚本,代码如下:
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import win32api
import win32con
import win32gui
import os
import time
def setWallpaper(image_path):
key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(key, "WallpaperStyle", 0, win32con.REG_SZ, "2")
win32api.RegSetValueEx(key, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,image_path, 1+2)
filepath = os.path.split(os.path.realpath(__file__))[0] + '\photo'
print(filepath)
image_list = os.listdir(filepath)
while True:
for image_name in image_list:
image_path = filepath + '\\' + image_name
setWallpaper(image_path)
time.sleep(10)
2.写一个批处理执行这个python脚本
@echo off
D:
cd D:\Python\src\github_practice\SetDesktopPaper
start pythonw SetDesktopPaper.py
exit
3.利用.vbs文件去执行上述批处理(这么操作主要是为了隐藏批处理运行窗口)
Set ws = CreateObject("Wscript.Shell")
ws.run "cmd /c D:\Python\src\github_practice\SetDesktopPaper\Startup.bat",vbhide
然后,我们把Startup.vbs文件设置桌面快捷方式,然后将这个快捷方式放入电脑自启动文件夹下。
这样,在我们每次开机后都会自动运行这个Startup.vbs文件,就实现了自动运行python脚本的功能。