吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 8577|回复: 36
收起左侧

[原创工具] Python之自动爬取每日bing壁纸并更换

  [复制链接]
yuyu226 发表于 2021-1-22 13:11
本帖最后由 yuyu226 于 2021-1-22 17:16 编辑

让大家久等了!
新人第一次发帖,请包容发帖,请包容


今天,我们来研究——Python自动爬取每日bing壁纸
说到Python想必大家对这个编辑器并不陌生。当然,最出名的,也就是他的爬虫

咳咳咳.......这里我就不详细逐一介绍了,我们直接开门见山{:301_1000:}

一、你需要准备
  • Python编译器
  • Pycharm(有条件者可以去官网买)
  • Pyinstaller,requests第三方库

二、使用Python自带的pip进行安装库
  • 同时摁下win+r键,输入cmd进入提示命令符
  • 输入 pip install ******
Tip: *号处可使用任何要按照第三方库的名称
这里只安装:requests和pyinstaller

三、函数__photo__.py爬取必应每日一图
[Python] 纯文本查看 复制代码
import re
import requests
import datetime
from __computerbackground__ import __computerbackground__
import os

def __photo__( ):
  url = 'https://www.bing.com/'
  html = requests.get(url).text
  #获取这个网页源码
  Nurl = re.findall('id="bgLink" rel="preload" href="(.*?)&',html,re.S)
  #正则表达式写好
  for temp in Nurl:

   #循环获取里边的图片,其实这里只有一个
        url = 'https://www.bing.com' + temp
        #print(url)
        pic = requests.get(url)
        a=(os.path.dirname(__file__) ) #获取当前路径

    #接着把图片保存下来,再提前准备一个Photo目录用来存放
        name = str(datetime.datetime.now().year)+'-'+str(datetime.datetime.now().month)+'-'+str(datetime.datetime.now().day) + '.jpg'
        file=a+'/Photo'+'/'+name #建立保存的绝对地址
        #print(file)
        while True:
            try:
                fp = open(file, 'wb')
                fp.write(pic.content)
                fp.close()  # 保存图片到Photo这个文件夹
                print('已将图片保存于', file)
                break
            except:
                print('发生错误!!请检查是否在', a, '中创建Photo名称的文件夹')
                b = input('创建确定请输入1:')
                                        #保存图片到

Tip:如果显示报错,你必须在运行本脚本的目录中创建一个Photo的文件夹,不然将无法保存!!

三、函数__computerbackground__.py更换壁纸
[Python] 纯文本查看 复制代码
import ctypes
import datetime
import time
import os

def __computerbackground__():
    #print(os.path.dirname(__file__))
    a=(os.path.dirname(__file__) )
    name=str(datetime.datetime.now().year)+'-'+str(datetime.datetime.now().month)+'-'+str(datetime.datetime.now().day) + '.jpg'
    time.sleep(1)
    file=a+"/Photo"+"/"+name
    #print(file)
    ctypes.windll.user32.SystemParametersInfoW(20,  0,file, 0)  #替换壁纸
    print('更换成功')


Tip:没有安装上ctypes库的将无法使用

三、主程序photo_get.py调用函数
[Python] 纯文本查看 复制代码
from __photo__ import __photo__
if __name__=='__main__':
    __photo__()


四、打包exe
  • 同时摁下win+r键,输入cmd进入提示命令符
  • 输入 cd D:/******      (注意:**处即为脚本photo_get.py运行所在文件夹)
  • 输入pyinstaller -D photo_get.py
[PHP] 纯文本查看 复制代码
Microsoft Windows [版本 10.0.17134.1304]
(c) 2018 Microsoft Corporation。保留所有权利。

(venv) D:\Pycharm 2020.3>pyinstaller -D photo_get.py
1465 INFO: PyInstaller: 4.2
1465 INFO: Python: 3.8.3
1465 INFO: Platform: Windows-10-10.0.17134-SP0
1465 INFO: wrote D:\Pycharm 2020.3\photo_get.spec
1513 INFO: UPX is not available.
1545 INFO: Extending PYTHONPATH with paths
['D:\\Pycharm 2020.3', 'D:\\Pycharm 2020.3']
1705 INFO: checking Analysis
1713 INFO: Building Analysis because Analysis-00.toc is non existent
1713 INFO: Initializing module dependency graph...
1729 INFO: Caching module graph hooks...
1785 WARNING: Several hooks defined for module 'win32ctypes.core'. Please take care they do not conflict.
1825 INFO: Analyzing base_library.zip ...
7761 INFO: Processing pre-find module path hook distutils from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks\\pre_find_module_path\\hook-d
istutils.py'.
7785 INFO: distutils: retargeting to non-venv dir 'D:\\Python3.8\\lib'
12161 INFO: Caching module dependency graph...
12417 INFO: running Analysis Analysis-00.toc
12497 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by d:\pycharm 2020.3\venv\scripts\python.exe
12609 INFO: Analyzing D:\Pycharm 2020.3\photo_get.py
12913 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe
_import_module\\hook-urllib3.packages.six.moves.py'.
16084 INFO: Processing module hooks...
16084 INFO: Loading module hook 'hook-certifi.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
16116 INFO: Loading module hook 'hook-difflib.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
16149 INFO: Excluding import of doctest from module difflib
16149 INFO: Loading module hook 'hook-distutils.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
16180 INFO: Loading module hook 'hook-distutils.util.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
16180 INFO: Excluding import of lib2to3.refactor from module distutils.util
16180 INFO: Loading module hook 'hook-encodings.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
16710 INFO: Loading module hook 'hook-heapq.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
16710 INFO: Excluding import of doctest from module heapq
16718 INFO: Loading module hook 'hook-lib2to3.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
16918 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
16942 INFO: Excluding import of test from module multiprocessing.util
16942 INFO: Excluding import of test.support from module multiprocessing.util
16942 INFO: Loading module hook 'hook-pickle.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
16966 INFO: Excluding import of argparse from module pickle
16966 INFO: Loading module hook 'hook-sysconfig.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
16990 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
16990 INFO: Loading module hook 'hook-xml.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
17102 INFO: Loading module hook 'hook-_tkinter.py' from 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks'...
18311 INFO: checking Tree
18311 INFO: Building Tree because Tree-00.toc is non existent
18311 INFO: Building Tree Tree-00.toc
18879 INFO: checking Tree
18879 INFO: Building Tree because Tree-01.toc is non existent
18879 INFO: Building Tree Tree-01.toc
19223 INFO: checking Tree
19223 INFO: Building Tree because Tree-02.toc is non existent
19223 INFO: Building Tree Tree-02.toc
19247 INFO: Looking for ctypes DLLs
19287 INFO: Analyzing run-time hooks ...
19287 INFO: Including run-time hook 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
19303 INFO: Including run-time hook 'd:\\pycharm 2020.3\\venv\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\rthooks\\pyi_rth_certifi.py'
19311 INFO: Looking for dynamic libraries
21526 INFO: Looking for eggs
21526 INFO: Using Python library D:\Python3.8\python38.dll
21526 INFO: Found binding redirects:
[]
21534 INFO: Warnings written to D:\Pycharm 2020.3\build\photo_get\warn-photo_get.txt
21605 INFO: Graph cross-reference written to D:\Pycharm 2020.3\build\photo_get\xref-photo_get.html
21654 INFO: checking PYZ
21654 INFO: Building PYZ because PYZ-00.toc is non existent
21654 INFO: Building PYZ (ZlibArchive) D:\Pycharm 2020.3\build\photo_get\PYZ-00.pyz
22502 INFO: Building PYZ (ZlibArchive) D:\Pycharm 2020.3\build\photo_get\PYZ-00.pyz completed successfully.
22517 INFO: checking PKG
22517 INFO: Building PKG because PKG-00.toc is non existent
22517 INFO: Building PKG (CArchive) PKG-00.pkg
22589 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
22589 INFO: Bootloader d:\pycharm 2020.3\venv\lib\site-packages\PyInstaller\bootloader\Windows-64bit\run.exe
22589 INFO: checking EXE
22589 INFO: Building EXE because EXE-00.toc is non existent
22589 INFO: Building EXE from EXE-00.toc
22605 INFO: Copying icons from ['d:\\pycharm 2020.3\\venv\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
22654 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
22654 INFO: Writing RT_ICON 1 resource with 3752 bytes
22661 INFO: Writing RT_ICON 2 resource with 2216 bytes
22661 INFO: Writing RT_ICON 3 resource with 1384 bytes
22661 INFO: Writing RT_ICON 4 resource with 37019 bytes
22661 INFO: Writing RT_ICON 5 resource with 9640 bytes
22661 INFO: Writing RT_ICON 6 resource with 4264 bytes
22661 INFO: Writing RT_ICON 7 resource with 1128 bytes
22685 INFO: Appending archive to EXE D:\Pycharm 2020.3\build\photo_get\photo_get.exe
22685 INFO: Building EXE from EXE-00.toc completed successfully.
22693 INFO: checking COLLECT
22693 INFO: Building COLLECT because COLLECT-00.toc is non existent
22693 INFO: Building COLLECT COLLECT-00.toc
38948 INFO: Building COLLECT COLLECT-00.toc completed successfully.


打包完成后,找到dist文件夹中的photo_get文件夹中的photo_get.exe双击即可运行换壁纸



你学会了吗????
如果不会使用,可直接下载下方的打包成品

——————————成品下载地址(小白专属)——————————————————

土豪VIP通道:
下载.txt (302 Bytes, 下载次数: 46)

小白通道:

链接:https://pan.baidu.com/s/1cwkwfCOIcYA6Ifg3gtM0vw
提取码:zzlq
更新时间:2021.1.22

————————————————————————————————————
新人发帖,存在不足之处请谅解

喜欢的记得好评加热心值哦,么么哒~~~~

免费评分

参与人数 10吾爱币 +14 热心值 +9 收起 理由
yndd81 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
醉舞春风 + 1 + 1 用心讨论,共获提升!
忧花殇倾城泪 + 1 我很赞同!
S4nqian + 1 热心回复!
水青兆走 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
1228小浣熊 + 1 + 1 热心回复!
13609971452 + 1 + 1 谢谢@Thanks!
brad + 1 + 1 我很赞同!
立刻 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
风之暇想 + 7 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| yuyu226 发表于 2021-1-22 16:00
本帖最后由 yuyu226 于 2021-1-22 16:16 编辑

新人第一次发帖,问题多多,请原谅
J_chen 发表于 2021-1-27 14:25
本帖最后由 J_chen 于 2021-1-27 14:57 编辑

简单研究了一下bing的壁纸,可以直接用api接口获取json:
https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&nc=1611072000000&pid=hp
其中参数:
idx=0 表示今天开始,1表示昨天,以此类推,最大为7;
n=1 表示要返回包括idx及前面几天的数据,每天1张 ,最大值为8;
nc= 当前13位时间戳
综合:最多可以提取到最近16天的图。
返回数据:
[JavaScript] 纯文本查看 复制代码
{"images":[{"startdate":"20210125","fullstartdate":"202101251600","enddate":"20210126","url":"/th?id=OHR.HeartAustralia_ZH-CN7049080558_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp","urlbase":"/th?id=OHR.HeartAustralia_ZH-CN7049080558","copyright":"属于大堡礁的一部分的心形礁,澳大利亚昆士兰 (© Peter Adams/Offset by Shutterstock)","copyrightlink":"/search?q=%e5%bf%83%e5%bd%a2%e7%a4%81&form=hpcapt&mkt=zh-cn","title":"","quiz":"/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20210125_HeartAustralia%22&FORM=HPQUIZ","wp":true,"hsh":"e314972f784c94703c223729aab5310e","drk":1,"top":1,"bot":1,"hs":[]}],"tooltips":{"loading":"正在加载...","previous":"上一个图像","next":"下一个图像","walle":"此图片不能下载用作壁纸。","walls":"下载今日美图。仅限用作桌面壁纸。"}}


解析:urlbase = /th?id=OHR.HeartAustralia_ZH-CN7049080558
要原图的话: https://cn.bing.com /th?id=OHR.HeartAustralia_ZH-CN7049080558 _UHD.jpg

免费评分

参与人数 2吾爱币 +3 热心值 +2 收起 理由
联盟少侠 + 2 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
yuyu226 + 1 + 1 我很赞同!

查看全部评分

 楼主| yuyu226 发表于 2021-1-30 20:47
J_chen 发表于 2021-1-27 14:25
简单研究了一下bing的壁纸,可以直接用api接口获取json:
https://cn.bing.com/HPImageArchive.aspx?forma ...

互相学习,我会过几天改造的,谢谢
 楼主| yuyu226 发表于 2021-1-22 17:53
chickenbaby 发表于 2021-1-22 17:45
支持原创,幸好论坛有你

感谢支持
立刻 发表于 2021-1-23 12:14
大佬能写一个centos服务器用的吗?
头像被屏蔽
youximang 发表于 2021-1-23 15:06
提示: 作者被禁止或删除 内容自动屏蔽
此用户已封停 发表于 2021-1-23 16:26
不错不错 这个是好东西 省心省事
irundream 发表于 2021-1-23 18:52

支持原创,幸好论坛有你
hsbxr 发表于 2021-1-23 19:00
好东东,收藏了
老哥稳6 发表于 2021-1-23 22:59
666666666666666666666666666666

免费评分

参与人数 1吾爱币 -5 收起 理由
swb8023 -5 请勿灌水,提高回帖质量是每位会员应尽的义务!

查看全部评分

luyinmao 发表于 2021-1-24 09:19
支持原创,幸好论坛有你!非常详细,再次感谢!
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-12-12 10:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表