1. 程式人生 > >Win7主題-通過Python製作跑車主題

Win7主題-通過Python製作跑車主題

1. 前期準備

1.1 開發工具

Python 3.6
Pycharm Pro 2017.3.2
Text文字

1.2 Python庫

requests
re
urllib

如果沒有這些Python庫,使用以下方法

pip install 需要安裝的包名(Ps: pip install requests)

2. 配置系統主題檔案

個人經過和系統主題對比寫了一個主題檔案程式碼,大家可以拷貝到text文字中另存為*.theme檔案,我這裡命名為lamborghini.theme

; Copyright ?Microsoft Corp.

[Theme]
; Windows 7 - IDS_THEME_DISPLAYNAME_AERO
DisplayName=蘭博基尼 # 個性化主題名稱
SetLogonBackground=0

; Computer - SHIDI_SERVER
[CLSID\
{20D04FE0-3AEA-1069-A2D8-08002B30309D}\DefaultIcon] DefaultValue=%SystemRoot%\System32\imageres.dll,-109 ; UsersFiles - SHIDI_USERFILES [CLSID\{59031A47-3F72-44A7-89C5-5595FE6B30EE}\DefaultIcon] DefaultValue=%SystemRoot%\System32\imageres.dll,-123 ; Network - SHIDI_MYNETWORK [CLSID\{F02C1A0D-BE21-4350-88B0-7367FC96EF3C
}\DefaultIcon] DefaultValue=%SystemRoot%\System32\imageres.dll,-25 ; Recycle Bin - SHIDI_RECYCLERFULL SHIDI_RECYCLER [CLSID\{645FF040-5081-101B-9F08-00AA002F954E}\DefaultIcon] Full=%SystemRoot%\System32\imageres.dll,-54 Empty=%SystemRoot%\System32\imageres.dll,-55 [Control Panel\Cursors] AppStarting=%SystemRoot%\cursors\aero_working.ani Arrow=%SystemRoot%\cursors\aero_arrow.cur Crosshair= Hand=%SystemRoot%\cursors\aero_link.cur Help=%SystemRoot%\cursors\aero_helpsel.cur IBeam= No=%SystemRoot%\cursors\aero_unavail.cur NWPen=%SystemRoot%\cursors\aero_pen.cur SizeAll=%SystemRoot%\cursors\aero_move.cur SizeNESW=%SystemRoot%\cursors\aero_nesw.cur SizeNS=%SystemRoot%\cursors\aero_ns.cur SizeNWSE=%SystemRoot%\cursors\aero_nwse.cur SizeWE=%SystemRoot%\cursors\aero_ew.cur UpArrow=%SystemRoot%\cursors\aero_up.cur Wait=%SystemRoot%\cursors\aero_busy.ani DefaultValue=Windows Aero
[email protected]
,-1020 [Control Panel\Desktop] Wallpaper=D:\Wallpaper\lamborghini\139_151202104128_86504.jpg # 初始化圖片 TileWallpaper=0 WallpaperStyle=10 Pattern= [VisualStyles] Path=%ResourceDir%\Themes\Aero\Aero.msstyles ColorStyle=NormalColor Size=NormalSize ColorizationColor=0XA84F1B1B Transparency=1 [boot] SCRNSAVE.EXE= [MasterThemeSelector] MTSM=DABJDKT [Sounds] ; IDS_SCHEME_DEFAULT [email protected]%SystemRoot%\System32\mmres.dll,-800 [Slideshow] Interval=60000 # 動畫時間 Shuffle=0 ImagesRootPath=D:\Wallpaper\ #圖片路徑 ----- 以下不要拷貝,用Python批量新增 ----- Item0Path=D:\Wallpaper\lamborghini\aventador_s-007.jpg Item1Path=D:\Wallpaper\lamborghini\aventador_s-006.jpg Item2Path=D:\Wallpaper\lamborghini\aventador_s-005.jpg Item3Path=D:\Wallpaper\lamborghini\aventador_s-004.jpg Item4Path=D:\Wallpaper\lamborghini\aventador_s-003.jpg Item5Path=D:\Wallpaper\lamborghini\aventador_s-002.jpg Item6Path=D:\Wallpaper\lamborghini\aventador_s-001.jpg
個性化主題配置檔案

3.獲取頁面地址

3.1 獲取需要爬取的網頁地址

url:http://www.ivsky.com/search.php?q=%E5%85%B0%E5%8D%9A%E5%9F%BA%E5%B0%BC&PageNo=2q 查詢的資料PageNo 頁碼

3.2 獲取爬取頁面分頁圖片地址

img_url:http://img.ivsky.com/img/bizhi/pic/201804/17/aventador_s-007.jpg
img_url:http://img.ivsky.com/img/bizhi/pre/201804/17/aventador_s-007.jpg
pic 原圖
pre 縮圖

4.編寫爬蟲

import requests, re, urllib.request
class Ivsky_Spider:
    def __init__(self, new_search_name):
        """初始化"""
        self.url_search = 'http://www.ivsky.com/search.php?q=%s' % urllib.request.quote(new_search_name) # 網站搜尋
        self.url = re.findall(r'(http://.*?)/', self.url_search)[0] # 網站地址
        self.headers = {
            'User-Agent': 'Mozilla/5.0', # 偽裝成瀏覽器訪問
            'Referer': self.url # 是否合法
        }

    def Spider(self):
        """主程式"""
        i = 1
        while True:
            try:
                print('='*30 + '第%d頁' % i + '='*30)
                respone = self.Get_Html_Respone(self.url_search + '&PageNo=' + str(i)).text
                page_temp = re.findall(r'<div class="pagelist">.*?</div>', respone, re.S)[0]
                if str(i) in page_temp:
                    self.Get_Img_Download(i, respone)
                else:
                    print('=' * 30 + '程式爬取完成' + '=' * 30)
                    return
                i += 1
            except Exception as e:
                print('報錯資訊:%s\n程式退出' % e)
                return

    def Get_Html_Respone(self, new_url):
        """網站Get請求"""
        respone = requests.get(url=new_url, headers=self.headers) # Get請求
        respone.encoding = 'utf-8' # 網頁編碼轉為utf-8
        return respone

    def Get_Img_Download(self, page, new_respone):
        """圖片下載"""
        print('-' * 20 + '正在獲取第%d頁圖片內容' % page + '-' * 20)
        img_url_temp = re.findall(r'<div class="left">.*?<ul class="pli">.*?</ul>', new_respone, re.S)[0]
        img_url_list = re.findall(r'<li>.*?<div.*?><a href="(.*?)".*?>', img_url_temp, re.S)
        for i in range(len(img_url_list)):
            print('-' * 20 + '正在下載第%d頁第%d張圖片' % (page, i+1) + '-' * 20)
            img_url = self.url + img_url_list[i]
            img_respone = self.Get_Html_Respone(img_url).text
            img_respone_url = re.findall(r"</script><img.*?src='(.*?)'", img_respone)[0].replace('pre', 'pic')
            img_f_name = img_respone_url[img_respone_url.rfind('/') + 1:]
            with open('D:\Wallpaper\lamborghini\%s' % img_f_name, 'wb') as f:
                img_result = self.Get_Html_Respone(img_respone_url).content
                f.write(img_result)
            with open('C:\\Users\Administrator\AppData\Local\Microsoft\Windows\Themes\lamborghini.theme', 'a') as f:
                f.write('\n')
                f.write('Item%dPath=D:\Wallpaper\lamborghini\%s' % (i, img_f_name))

if __name__ == '__main__':
    search_name = u'蘭博基尼'
    a = Ivsky_Spider(search_name)
    a.Spider()