python專用桌布 Python爬Mac專用桌布

2021-10-18 14:23:42 字數 4037 閱讀 8177

該樓層疑似違規已被系統摺疊 隱藏此樓檢視此樓

很多人都知道,網路上有很多的桌布,但是大部分都是1980*1080的解析度,可是這專為mac系統做的桌布確實5000+*3000+的解析度,你可以想象一下兩種桌布的差距,**如下

# 獲取乙個** pip install requests

from requests import get

# 判斷檔案型別的第三方包 pip install filetype

from filetype import guess

# 命名檔案或者目錄

from os import rename

# 建立目錄的包

from os import makedirs

# 判斷檔案是否存在

from os.path import exists

# 將已經編碼的json字串解碼為python物件

from json import loads

# 上下文管理器操作模組 當我們請求api的時候,如果程式執行完成了,就關閉鏈結

from contextlib import closing

1.獲取資源

def down_load(file_url, file_path, now_wall*****_count, all_wall*****_count):

headers = {

"user-agent":

with closing(get(file_url, headers=headers, stream=true)) as response:

# 單次請求最大值

chunk_size = 1024

# 檔案總大小 這個引數在瀏覽器響應頭中

content_size = int(response.headers['content-length'])

# 初始化當前已經傳輸的大小

data_count = 0

# 檔案操作

if response.status_code == 200:

with open(file_path, 'wb') as file:

for data in response.iter_content(chunk_size=chunk_size):

file.write(data)

done_block = int((data_count / content_size) * 50)

data_count = data_count + len(data)

now_percentage = (data_count / content_size) * 100

print("\r %s: [%s%s] %d%% %d/%d" % (file_path, done_block * '*', ' ' * (50 - 1 - done_block), now_percentage, now_wall*****_count, all_wall*****_count), end=' ')

file_pype = guess(file_path)

try:

rename(file_path, file_path + '.' + file_pype.extension)

except fileexistserror:

print('該資源已經存在...')

rename(file_path, file_path + '副本' + '.' + file_pype.extension)

# 獲取資源

def spider_wall*****(type_id, wall*****_count):

url = ''

if(type_id == 1):

url = '' + str(wall*****_count)

elif(type_id == 2):

url = '' + str(wall*****_count)

elif(type_id == 3):

url = '' + str(wall*****_count)

elif(type_id == 4):

url = '' + str(wall*****_count)

# 因為當我們用爬蟲程式去訪問**的時候,那個**會檢測你是不是用瀏覽器

# 去訪問的,如果不是,會禁止你訪問

# 模擬瀏覽器向**發請求

headers = {

"user-agent":

response = get(url, headers=headers)

# 把json資料轉成python物件

wall*****_data = loads(response.content)

now_wall*****_count = 1

# 所有的張數

all_wall*****_count = len(wall*****_data)

make_dir = ''

for wall***** in wall*****_data:

if type_id == 1:

if not exists('./' + '最新桌布'):

makedirs('./' + '最新桌布')

make_dir = '最新桌布'

elif type_id == 2:

if not exists('./' + '最熱桌布'):

makedirs('./' + '最熱桌布')

make_dir = '最熱桌布'

elif type_id == 3:

if not exists('./' + '女生桌布'):

makedirs('./' + '女生桌布')

make_dir = '女生桌布'

elif type_id == 4:

if not exists('./' + '星空桌布'):

makedirs('./' + '星空桌布')

make_dir = '星空桌布'

# print(wall*****)

file_url = wall*****['urls']['raw']

file_name_only = file_url.split('/')

# 以最後乙個url引數作為桌布名稱

file_name_only = file_name_only[len(file_name_only) - 1]

file_path = './' + make_dir + '/' + file_name_only

down_load(file_url, file_path, now_wall*****_count, all_wall*****_count)

print('\t' + file_name_only)

now_wall*****_count += 1

if __name__== "__main__":

while(true):

print('\n\n')

# 選擇桌布型別

wall_*****_id = input('請輸入桌布型別(數字) [1.最新桌布 2.最熱桌布 3.女生桌布 4.星空桌布]:')

# 判斷輸入是否正確

while (wall_*****_id != str(1) and wall_*****_id != str(2) and wall_*****_id != str(3) and wall_*****_id != str(4)):

wall_*****_id = input('請重新輸入桌布型別(數字) [1.最新桌布 2.最熱桌布 3.女生桌布 4.星空桌布]:')

# 判斷輸入是否正確

while(int(wall_*****_count) <= 0):

# 開始爬取5k超清桌布

spider_wall*****(int(wall_*****_id), int(wall_*****_count))

break

下圖是怎樣在瀏覽器上獲取"user-agent":,你必須要開啟你要爬取東西的頁面上才行,這只是乙個例項,並不是我要爬取的頁面

python專用桌布 Python爬Mac專用桌布

該樓層疑似違規已被系統摺疊 隱藏此樓檢視此樓 很多人都知道,網路上有很多的桌布,但是大部分都是1980 1080的解析度,可是這專為mac系統做的桌布確實5000 3000 的解析度,你可以想象一下兩種桌布的差距,如下 獲取乙個 pip install requests from requests ...

python 專用方法

下面的例子將展示 userdict 一些其他專用方法。def repr self return repr self.data cmp self,dict return cmp self.data,dict.data else return cmp self.data,dict def len sel...

Python的專用方法

原文引自 python 類可以定義專用方法,專用方法是在特殊情況下或當使用特別語法時由 python 替你呼叫的,而不是在 中直接呼叫 象普通的方法那樣 1 init 類似於建構函式 class study def init self,name none self.name name def say...