1. 程式人生 > >python 獲取封面圖 簡單示例

python 獲取封面圖 簡單示例

根據 content tex [1] none 指定 for where word

#coding=utf-8
#Python Version python3
#Desciption
#python paperwalls.py serach keyword  download dir eg:python 1.py  kaori ../resource/wallpaper
#download_pic  下載圖片
#get_download_link 獲取下載的鏈接
#getwallpaper 獲取圖片 保存到本地
#缺點: 沒有用多線程  細節處理不是很好 沒有一般通用性

import requests
import re
import os
import sys

# 代理地址
proxies = { "http": "http://127.0.0.1:25378", "https": "http://127.0.0.1:25378", }
# 下載的目錄
download_dir = ‘../resource/wallpaper/‘
#已經下載的數量
downloaded_num = 0
# 總共搜索的到的數量
total = 0


#下載圖片
def download_pic(url, name, pic_type):
    #獲取全局的變量
    global proxies
    global download_dir
    global downloaded_num
    global total
    # 目錄不存在創建目錄下載圖片 
    if not os.path.exists(download_dir):
        os.makedirs(download_dir)
    # 下載圖片到指定的目錄
    r = requests.get(url, =proxiesproxies)
    # 請求成功 已下載數+1
    downloaded_num += 1
    #打開文件 寫入圖片
    with open(‘%s/%s.%s‘%(download_dir, name, pic_type), ‘wb‘) as f:
        f.write(r.content)
    #格式化輸出下載結果 eg:[  101/817] 629538.jpg Done!
    print(‘[{:5d}/{}] {}.{} Done!‘.format(downloaded_num, total, name, pic_type))


#下載鏈接
def get_download_link(wallpaper_id, wallpaper_type, server, user_id):
    global proxies
    post_data = {
        ‘wallpaper_id‘: wallpaper_id,
        ‘type‘: wallpaper_type,
        ‘server‘: server,
        ‘user_id‘: user_id,
    }
    r = requests.post(‘https://wall.alphacoders.com/get_download_link.php‘, data=post_data, proxies=proxies)
    download_pic(r.text, wallpaper_id, wallpaper_type)
    
#根據關鍵詞獲取壁紙
def getwallpaper(keyword):
    global proxies
    global total
    #正則匹配頁數
    p_nextpage = re.compile(r"<a id=‘next_page‘ href=[\‘\"](.+?)[\‘\"]>")
    #獲取POST表單數據 \d+? 一個或多個數字  \w+? 一個或多個字符
    p_item = re.compile(r‘data-id="(\d+?)" data-type="(\w+?)" data-server="(\w+?)" data-user-id="(\d+?)"‘)
    #設置當前頁為第一頁
    page_num = 1
    #循環
    while 1:
        r_page = requests.get(‘https://wall.alphacoders.com/search.php?search=%s&lang=Chinese&page=%d‘ % (keyword.lower(), page_num), proxies=proxies)
        nextpage_link = p_nextpage.search(r_page.text)
        
        # 如果沒有查詢到結果  跳出循環
        if nextpage_link == None:
            print("Sorry, we have no results for your search!")
            break
        if page_num == 1:
            # 獲取第一個返回值 總數
            total = int(re.search(r"<h1 class=‘center title‘>\s+?(\d+)(.+?)\s+?</h1>", r_page.text).group(1))
            print("the %s wallpaper‘s total is %d" % (keyword, total))
        for item in p_item.findall(r_page.text):
            wallpaper_id = item[0]
            wallpaper_type = item[1]
            server = item[2]
            user_id = item[3]
            get_download_link(wallpaper_id, wallpaper_type, server, user_id)
        
        # 沒有下一頁了 跳出循環
        if nextpage_link.group(1) == ‘#‘:
            print("All wallpaper done!")
            break
        
        page_num += 1
   

#主函數        
if __name__ == ‘__main__‘:
    if len(sys.argv) < 2 or len(sys.argv) > 3:
        usage_text = "Usage:\n\tpython getwallpaper.py miku [miki_pic]\nFirst param: the name of script\nSecond param: the wallpaper‘s keyword which you want to search\nThird param: the dir‘s name where you want to download in, optional, default in ../resource/wallpapers"
        print(usage_text)
    elif len(sys.argv) == 3:
        download_dir = str(sys.argv[2])
        getwallpaper(str(sys.argv[1]))
    else:
        getwallpaper(str(sys.argv[1]))

  

python 獲取封面圖 簡單示例