Python之自动爬取每日bing壁纸并更换

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

咳咳咳…….这里我就不详细逐一介绍了,我们直接开门见山

一、你需要准备

  • Python编译器
  • Pycharm(有条件者可以去官网买)
  • Pyinstaller,requests第三方库

二、使用Python自带的pip进行安装库

  • 同时摁下win+r键,输入cmd进入提示命令符
  • 输入 pip install ******

Tip: *号处可使用任何要按照第三方库的名称
这里只安装:requests和pyinstaller

三、函数__photo__.py爬取必应每日一图

[Python] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
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(200,file, 0#替换壁纸
    print('更换成功')

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

三、主程序photo_get.py调用函数

[Python] 纯文本查看 复制代码
1
2
3
from __photo__ import __photo__
if __name__=='__main__':
    __photo__()

四、打包exe

  • 同时摁下win+r键,输入cmd进入提示命令符
  • 输入 cd D:/******      (注意:**处即为脚本photo_get.py运行所在文件夹)
  • 输入pyinstaller -D photo_get.py
[PHP] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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免费

已有0人支付

冰冰资源网免费提供各种商业源码下载。
冰冰资源网 » Python之自动爬取每日bing壁纸并更换

冰冰资源网_PHP秘籍__微信小程序_PHP网站源码_免费下载,活动期间开通年费会员最低仅需29元

交流QQ群:196268283

立即查看 了解详情