长时间坐在电脑面前你眼睛会不会酸呢,那么今天分享的是一个换美女桌面壁纸的神操作,给大家养养眼

随机更换美女桌面壁纸(可修改自己喜欢的风格桌面壁纸)

目前可以随即选取4个网站中一张美女图片,来作为壁纸。

import random
import requests
from lxml import etree
from fake_useragent import UserAgent
import win32api
import win32gui
import win32con
import os
import re
import urllib
 
def set_wallpaper(bmp_path):
    # 打开指定注册表路径
    reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
    # 最后的参数:2拉伸,0居中,6适应,10填充,0平铺
    win32api.RegSetValueEx(reg_key, "WallpaperStyle", 0, win32con.REG_SZ, "2")
    # 最后的参数:1表示平铺,拉伸居中等都是0
    win32api.RegSetValueEx(reg_key, "TileWallpaper", 0, win32con.REG_SZ, "0")
    # 刷新桌面
    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,bmp_path, win32con.SPIF_SENDWININICHANGE)
class bizhi_360():
    def __init__(self):
        bizhi_360 = ['http://bizhi360.com/meinv/list_{}.html'.format(i) for i in range(48)]
        bizhi_360_one = random.choice(bizhi_360)
        wang_zhan = [bizhi_360_one]
        self.a = random.choice(wang_zhan)
    def huoqu(self):
 
          # 模拟访问者
        ua = UserAgent().random
        headers = {"User-Agent": ua}
        data = requests.get(url=self.a, headers=headers)
        data.encoding= "utf-8"
        data= data.text
        s = etree.HTML(data)
        meinv_liebiao = s.xpath('//*[@id="main"]/div/div[1]/ul/li/a//@href')
        if len(meinv_liebiao) >1 :
            meinv = random.choice(meinv_liebiao)
            s = 'http://bizhi360.com'
            url = s + meinv
            self.xiazai(url)
        else:
            pass
    def xiazai(self,url):
        ua = UserAgent().random
        headers = {"User-Agent": ua}
        data = requests.get(url=url, headers=headers)
        data.encoding = "utf-8"
        data =data.text
        s = etree.HTML(data)
        meinv_tupian = s.xpath('//*[@id="main"]/div/div[1]/div[2]/ul/li/a//@href')
        if len(meinv_tupian) >1 :
            meinv = random.choice(meinv_tupian)
            s = 'http://bizhi360.com'
            url1 = s + meinv
            print(url1)
            self.xiazai_tupian(url1)
        else:
            meinv_url = s.xpath('//*[@id="main"]/div/div[1]/div[2]/figure/a//@href')
            try:
                url = meinv_url[0]
                response = requests.get(url,headers=headers)
                # 获取的文本实际上是图片的二进制文本
                img = response.content
                # 将他拷贝到本地文件 w 写  b 二进制  wb代表写入二进制文本
                # 保存路径
                path1 = os.getcwd()
                path = path1 + '\\1.jpg'
                with open(path, 'wb') as f:
                    f.write(img)
            except Exception as ex:
                print("出错")
 
    def xiazai_tupian(self,url1):
        ua = UserAgent().random
        headers = {"User-Agent": ua}
        data = requests.get(url=url1, headers=headers)
        data.encoding = "utf-8"
        data = data.text
        s = etree.HTML(data)
        meinv_tupian = s.xpath('//*[@id="main"]/div[1]/div[1]/div[2]/figure/a//@href')
        try:
            url = meinv_tupian[0]
            response = requests.get(url,headers=headers)
            # 获取的文本实际上是图片的二进制文本
            img = response.content
            # 将他拷贝到本地文件 w 写  b 二进制  wb代表写入二进制文本
            # 保存路径
            path1 = os.getcwd()
            path = path1+ '\\1.jpg'
            with open(path, 'wb') as f:
                f.write(img)
        except Exception as ex:
            print("--------出错继续----")
 
 
class k4_bizhi():
    def __init__(self):
        num = random.randint(1,171)
        bizhi_360 = ('http://pic.netbian.com/4kmeinv/index_{}.html'.format(num))
        self.a = bizhi_360
    def huoqu(self):
        ua = UserAgent().random
        headers = {"User-Agent": ua}
        data = requests.get(url=self.a, headers=headers)
        data.encoding = "utf-8"
        data = data.text
        s = etree.HTML(data)
        meinv_liebiao = s.xpath('//*[@id="main"]/div[3]/ul/li/a//@href')
        if len(meinv_liebiao) >1 :
            meinv = random.choice(meinv_liebiao)
            s = 'http://pic.netbian.com'
            url = s + meinv
            self.xiazai(url)
        else:
            pass
    def xiazai(self,url):
        ua = UserAgent().random
        headers = {"User-Agent": ua}
        data = requests.get(url=url, headers=headers)
        data.encoding = "utf-8"
        data = data.text
        s = etree.HTML(data)
        meinv_tupian = s.xpath('//*[@id="img"]/img//@src')
        try:
            s = 'http://pic.netbian.com'
            url1 = s + meinv_tupian[0]
            response = requests.get(url1, headers=headers)
            # 获取的文本实际上是图片的二进制文本
            img = response.content
            # 将他拷贝到本地文件 w 写  b 二进制  wb代表写入二进制文本
            # 保存路径
            path1 = os.getcwd()
            path = path1 + '\\1.jpg'
            with open(path, 'wb') as f:
                f.write(img)
        except Exception as ex:
            print("--------出错继续----")
 
class k4_weimei():
    def __init__(self):
        num = random.randint(1, 6)
        bizhi_360 = ('http://www.4kbizhi.com/meinv/index_{}.html'.format(num))
        self.a = bizhi_360
    def huoqu(self):
 
          # 模拟访问者
        ua = UserAgent().random
        headers = {"User-Agent": ua}
        data = requests.get(url=self.a, headers=headers)
        data.encoding= "utf-8"
        data= data.text
        s = etree.HTML(data)
        meinv_liebiao = s.xpath('/html/body/div[2]/div[1]/ul/li/a//@href')
        meinv_liebiao1 = s.xpath('/html/body/div[2]/div[2]/ul/li/a//href')
        meinv_lie = meinv_liebiao +meinv_liebiao1
        if len(meinv_lie) >1 :
            meinv = random.choice(meinv_liebiao)
            meinv= meinv.replace('.html', '')
            s = 'http://www.4kbizhi.com'
            url = s + meinv + '-1920x1080' +'.html'
            self.xiazai(url)
        else:
            pass
    def xiazai(self,url):
        ua = UserAgent().random
        headers = {"User-Agent": ua}
        data = requests.get(url=url, headers=headers)
        data.encoding = "utf-8"
        data =data.text
        s = etree.HTML(data)
        meinv_tupian = s.xpath('/html/body/div[2]/img//@src')[0]
        b = meinv_tupian.replace('small', '')
        c = b.split('/')
        c.pop()
        c = '/'.join(c)
        b = b.split('/')[-1]
        b = b[:11]
        meinv_tupian_url = c + '/' + b + '.jpg'
        try:
            s = 'http://www.4kbizhi.com'
            url1 = s + meinv_tupian_url
            response = requests.get(url1,headers=headers)
            # 获取的文本实际上是图片的二进制文本
            img = response.content
            # 将他拷贝到本地文件 w 写  b 二进制  wb代表写入二进制文本
            # 保存路径
            path1 = os.getcwd()
            path = path1 + '\\1.jpg'
            with open(path, 'wb') as f:
                f.write(img)
        except Exception as ex:
            print("出错")
class tupian_zhijia():
    def __init__(self):
        num = random.randint(1, 11)
        bizhi_360 = ('https://www.tupianzj.com/bizhi/DNmeinv/list_77_{}.html'.format(num))
        self.a = bizhi_360
    def huoqu(self):
 
          # 模拟访问者
        ua = UserAgent().random
        headers = {"User-Agent": ua}
        data = requests.get(url=self.a, headers=headers)
        data.encoding= "utf-8"
        data= data.text
        s = etree.HTML(data)
        meinv_liebiao = s.xpath('//*[@id="container"]/div/div/div[3]/div/ul/li/a//@href')
 
        if len(meinv_liebiao) >1 :
            meinv = random.choice(meinv_liebiao)
            meinv= meinv.replace('.html', '')
            s = 'https://www.tupianzj.com'
            url = s + meinv + '_1920x1080' +'.html'
            self.xiazai(url)
        else:
            pass
    def xiazai(self,url):
        ua = UserAgent().random
        headers = {"User-Agent": ua}
        data = requests.get(url=url, headers=headers)
        data.encoding = "utf-8"
        data = data.text
        meinv_tupian = re.findall(r"if\(imgSize==\"1920x1080\"\) picurl='.*",data)
        meinv_tupian = ''.join(meinv_tupian)
        meinv_tupian = meinv_tupian.replace('\'', '')
        meinv_tupian = meinv_tupian.replace(';', '')
        meinv_tupian =  meinv_tupian.split('=')[-1]
        meinv_tupian1 = ''.join(meinv_tupian)
        self.xiazai_tupian(meinv_tupian1)
    def xiazai_tupian(self,url1):
        try:
            ua = UserAgent().random
            url = str(url1)
            headers = {"User-Agent": ua}
            request = urllib.request.Request(url, headers=headers)
            response = urllib.request.urlopen(request)
            # 获取的文本实际上是图片的二进制文本
            img = response.read()
            # response = requests.get(url, headers=headers)
            # img = response.content
            # 将他拷贝到本地文件 w 写  b 二进制  wb代表写入二进制文本
            # 保存路径
            path1 = os.getcwd()
            path = path1 + '\\1.jpg'
            with open(path, 'wb') as f:
                f.write(img)
        except Exception as ex:
            print("出错")
 
 
if __name__== '__main__':
    liebiao = [1,2,3,4]
    a= random.choice(liebiao)
    if a ==1:
        bizhi_360().huoqu()
    elif a==2:
        k4_bizhi().huoqu()
    elif a ==3:
        k4_weimei().huoqu()
    elif a==4:
        tupian_zhijia().huoqu()
    path1 = os.getcwd()
    path = path1 +"\\1.jpg"  # 图片路径
    try:
        set_wallpaper(path)
    except:
        print('图片为下载!')

自己可以打包成exe文件,用win10自带的任务工具,设置成多久启动一次。也可以写成bat文件,在写成vbs启动bat文件去除代码框,放在桌面双击就换一站美女桌面壁纸

应部分粉丝要求,怕有些人不会用这些代码,那就用 pyinstaller打包成exe可执行文件,有点大,这是python打包的缺点。

下载:https://wws.lanzous.com/i2KAGjtayob 密码:ah95

文章名: 《Python随机更换美女桌面壁纸》
本站为非盈利性个人博客
本站部分资源整合转载于网络,如果侵犯了您的权利,请及时联系我删除!
如果我的文章打动了你的内心或者对你有帮助请不妨打赏我一杯咖啡!
Last modification:December 28th, 2020 at 11:31 pm
如果觉得我的文章对你有用,请随意赞赏