python3爬取壁纸图片,python3切换壁纸

#!/usr/bin/python
# encoding=utf-8

import requests
from bs4 import BeautifulSoup

class bizhi:
    def getheaders(self):
        return {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Host": "www.bizhi360.com",
            "Referer": "http://www.bizhi360.com/meinv/",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36",
        }
    def getHtml(self,url):
        r = requests.get(url,headers=self.getheaders())
        result = r.content.decode('utf-8')
        html = BeautifulSoup(result,'html.parser')
        ul = html.find('ul')
        all_li = ul.find_all('li')
        for i in all_li:
            a = "http://www.bizhi360.com"+i.find('a').get('href')
            # 详情页
            details_con = requests.get(a,headers=self.getheaders())
            details_html = BeautifulSoup(details_con.content.decode('utf-8'),'html.parser')
            dul = details_html.find('div',class_='pic-list')
            if dul:
                all_dli = dul.find_all('li')
                if all_dli:
                    #多图
                    for di in all_dli:
                        xurl = 'http://www.bizhi360.com'+di.find('a').get('href')
                        img_html = requests.get(xurl, headers=self.getheaders())
                        self.getImg(img_html)

            else:
                self.getImg(details_con)
    def imgheader(self):
        return {
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cache-Control": "max-age=0",
            "Connection": "keep-alive",
            "Host": "pic.bizhi360.com",
            "If-Modified-Since": "Wed, 01 Sep 2021 14:24:33 GMT",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36",
        }
    def getImg(self,img_html):
        details_html = BeautifulSoup(img_html.content.decode('utf-8'), 'html.parser')
        img_url = details_html.find('figure').find('a').get('href')
        title = details_html.find('title').getText()
        img_html = requests.get(img_url, headers=self.imgheader())
        img_html_c = img_html.content
        ext = img_url.split('.') # 获取后缀名
        with open('./img/' + str(title) + "." + str(ext[3]), 'wb') as f:
            f.write(img_html_c)
            f.close()

if __name__ == '__main__':
    x = bizhi()
    # x.getHtml("http://www.bizhi360.com/meinv") #http://www.bizhi360.com/meinv/list_2.html
    for i in range(2,100):
        try:
            x.getHtml(
                "http://www.bizhi360.com/meinv/list_" + str(i) + ".html")  # http://www.bizhi360.com/meinv/list_2.html
        except Exception:
            print('1')



定时切换壁纸代码

#!/usr/bin/python
# encoding=utf-8
import threading
import os
import random
import ctypes

def change_background():
    # 注意文件夹路径
    rootdir = 'c:/www/python/img'
    for parent, dirnames, filenames in os.walk(rootdir):
        file_names = filenames
        x = random.randint(0, len(file_names)-1)
        file_path = rootdir+"/"+filenames[x]
        ctypes.windll.user32.SystemParametersInfoW(20, 0, file_path, 3)

def timer():
    # 5分钟切换一次
    t = threading.Timer(300,change_background)
    t.start()
if __name__ == '__main__':
    timer()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值