Python automatically replaces "computer wallpaper"

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only. They do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it yourself

Python free learning materials and group communication answers Click to join


This article is divided into two stages. The first stage is to download the computer wallpaper, because the editor does not have a picture wallpaper, so I wrote a crawler to crawl the other shore wallpaper; the second stage is to realize the operation of automatically changing the computer wallpaper, if Readers have pictures and wallpapers, so they can watch the second stage directly.

Crawler get wallpaper

This is a crawling process, but automation is applied, that is, the selenium module in python. The required python modules are requests, selenium, os, time, bs4, etc.

Destination URL: http://pic.netbian.com/

We enter the content we want to search in the input box, click search, we can find that the URL format at this time is like this.

That is, the entered keyword has become a search id, so how to get this URL? The editor is like this, apply it to the selenium module to automate the search, come to the interface of this website, and then apply the current_url method under the selenium module to get the website.

Below this URL is the picture we searched for. Obviously, these pictures can't have only one page (except for ajax). It is found that there is a total page number under this URL, such as the total page number below is 191 pages.

If we click on one of the page numbers, we can find it. The next URL is: http://pic.netbian.com/e/search/result/index.php?page=1&searchid=18  , and the parameters behind the page It is one less than the actual number of pages, that is, when I select the first page, the actual page=0. Let’s click on the picture to take a look (of course it’s in the case of the punch-in developer mode)

I found that the size of this picture is relatively small. If you download it directly in this way and set it as a wallpaper, the computer desktop will definitely be blurred.

After several attempts, it was found that there is also a download address for this picture in another website. The picture size is relatively large, and the phenomenon will not happen when set as the computer wallpaper.

After that, we only need to get this URL and download it. Part of the code is as follows:

def Download():
    id,path=get_info()
    # http://pic.netbian.com/e/search/result/index.php?page={}&searchid={}
    #  网址的格式
    url2='http://pic.netbian.com/e/search/result/index.php?page={}&searchid={}'

    headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36'}
    # 请求头
    page2=int(input('请输入下载页数(注意不要超过总页数):'))  # 下载页数

    for page in range(page2):
        url=url2.format(page,id)
        content=requests.get(url=url,headers=headers)
        L2=bs(content=content.text)

        for j in range(len(L2)):
            content=requests.get(url=L2[j],headers=headers).content
            with open(file=path+'./{}-{}.jpg'.format(page+1,j),mode='wb') as f:
                f.write(content)
        print('-->已下载{}页'.format(page+1))
        time.sleep(2)  # 每下载一页,休眠2秒

Automatically change computer wallpaper. The
required python modules are win32api, win32con, win32gui, os, time, etc. This basic principle seems to be applied to the computer registry, I don't understand it very well, so I won't explain it clearly here, and I will directly upload the code.

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)
    while True:
        Windows_img(path+'\{}'.format(L2[i]))
        time.sleep(10)  # 设置壁纸更换间隔,这里为10秒,根据用户自身需要自己设置秒数
        i += 1
        if i==len(L2):  # 如果是最后一张图片,则重新到第一张
            i=0

if __name__ == '__main__':
    changeWallpaper()

This time in the code can be set according to your own needs.

 

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/112831596