python皮膚文件獲取腳本分享
以下是腳本,錯(cuò)誤幾乎遇到的全部都有處理,編寫(xiě)不易,還望點(diǎn)個(gè)小贊~~(注:要先下載pyautogui第三方庫(kù),命令:pip install pyautogui)
------------------------------------------------------------------
import requests
import re
import base64
import _thread
import pyautogui
from time import sleep
import os
import time
print(f"{time.strftime('%H:%M:%S', time.localtime())} ?正在準(zhǔn)備.....")
# 變量定義
name_list = open(input("請(qǐng)輸入皮膚名文本文件位置"))
log = open("請(qǐng)輸入日志文本文件位置(注:要先創(chuàng)建好文件)", "a+")
print_pond = []
pan = True
list = name_list.readlines()
run = True
skin_pond = []
start = 426618
theard = 0
header1 = {
? ? "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)"
? ? " AppleWebKit/537.36 (KHTML, like Gecko)"
? ? " Chrome/92.0.4515.131"
? ? " Safari/537.36 Edg/92.0.902.67",
? ? "Accept": "text/html,application/xhtml+xml,application/xml;"
? ? "q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
? ? "Accept-Encoding": "gzip, deflate, br",
? ? "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
? ? "Cache-Control": "max-age=0",
? ? "Connection": "keep-alive",
? ? "Host": "api.mojang.com",
? ? "sec-ch-ua": '"Chromium";v="92", " Not A;Brand";v="99", "Microsoft Edge";v="92"',
? ? "sec-ch-ua-mobile": "?0",
? ? "Sec-Fetch-Dest": "document",
? ? "Sec-Fetch-Mode": "navigate",
? ? "Sec-Fetch-Site": "none",
? ? "Sec-Fetch-User": "?1",
? ? "Upgrade-Insecure-Requests": "1"
}
header2 = {"authority": "sessionserver.mojang.com",
? ? ? ? ? ?"method": "GET",
? ? ? ? ? ?"path": "/session/minecraft/profile/75999265ff224440a192440615475d9c",
? ? ? ? ? ?"scheme": "https",
? ? ? ? ? ?"accept": "text/html,application/xhtml+xml,application/xml;"
? ? ? ? ? ?"q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
? ? ? ? ? ?"accept-encoding": "gzip, deflate, br",
? ? ? ? ? ?"accept-language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
? ? ? ? ? ?"cache-control": "max-age=0",
? ? ? ? ? ?"sec-ch-ua": '"Chromium";v="92", " Not A;Brand";v="99", "Microsoft Edge";v="92"',
? ? ? ? ? ?"sec-ch-ua-mobile": "?0",
? ? ? ? ? ?"sec-fetch-dest": "document",
? ? ? ? ? ?"sec-fetch-mode": "navigate",
? ? ? ? ? ?"sec-fetch-site": "none",
? ? ? ? ? ?"sec-fetch-user": "?1",
? ? ? ? ? ?"upgrade-insecure-requests": "1",
? ? ? ? ? ?"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
? ? ? ? ? ?"AppleWebKit/537.36 (KHTML, like Gecko) "
? ? ? ? ? ?"Chrome/92.0.4515.131 "
? ? ? ? ? ?"Safari/537.36 "
? ? ? ? ? ?"Edg/92.0.902.67"
? ? ? ? ? ?}
# 變量定義
# 函數(shù)定義
def gtime(): ?# 獲取當(dāng)前時(shí)間
? ? return time.strftime(r"%H:%M:%S", time.localtime())+" ?"
def html_get(url, the_header, id, error1="網(wǎng)絡(luò)不可用!", error2="錯(cuò)誤!(網(wǎng)站已響應(yīng),數(shù)據(jù)無(wú)效!)"): ?# 獲取html
? ? global pan
? ? while not pan: ?# 在錯(cuò)誤解除之前一直等待
? ? ? ? sleep(1)
? ? is_one = False
? ? not_one = False
? ? htm = None
? ? code = ""
? ? text = ""
? ? to_try = 0
? ? while True:
? ? ? ? try:
? ? ? ? ? ? htm = requests.get(url, headers=the_header, timeout=8)
? ? ? ? ? ? htm = str(htm.text)
? ? ? ? ? ? code = "OK"
? ? ? ? ? ? try: ?# 錯(cuò)誤檢測(cè)
? ? ? ? ? ? ? ? # 網(wǎng)站響應(yīng),但無(wú)數(shù)據(jù)
? ? ? ? ? ? ? ? if htm == "":
? ? ? ? ? ? ? ? ? ? code = "error2"
? ? ? ? ? ? ? ? # 內(nèi)部服務(wù)器錯(cuò)誤
? ? ? ? ? ? ? ? if htm == '{"error":"Internal Server Error","errorMessage":"The server encountered an unexpected condition which prevented it from fulfilling the request"}':
? ? ? ? ? ? ? ? ? ? pan = False
? ? ? ? ? ? ? ? ? ? print(gtime()+f"get_skin_thread(id:{id}):內(nèi)部服務(wù)器錯(cuò)誤,等待10秒后再次嘗試")
? ? ? ? ? ? ? ? ? ? sleep(10)
? ? ? ? ? ? ? ? ? ? raise OSError
? ? ? ? ? ? ? ? # 請(qǐng)求過(guò)多
? ? ? ? ? ? ? ? if htm == '{"error":"TooManyRequestsException","errorMessage":"The client has sent too many requests within a certain amount of time"}':
? ? ? ? ? ? ? ? ? ? while not pan: ?# 在錯(cuò)誤解除之前一直等待
? ? ? ? ? ? ? ? ? ? ? ? if is_one:
? ? ? ? ? ? ? ? ? ? ? ? ? ? break
? ? ? ? ? ? ? ? ? ? ? ? sleep(1)
? ? ? ? ? ? ? ? ? ? ? ? not_one = True
? ? ? ? ? ? ? ? ? ? pan = False
? ? ? ? ? ? ? ? ? ? if not not_one:
? ? ? ? ? ? ? ? ? ? ? ? is_one = True
? ? ? ? ? ? ? ? ? ? text = text + "\n" + \
? ? ? ? ? ? ? ? ? ? ? ? gtime()+f"get_skin_thread(id:{id}):請(qǐng)求過(guò)多!等待10秒后再次嘗試"
? ? ? ? ? ? ? ? ? ? print(gtime()+f"get_skin_thread(id:{id}):請(qǐng)求過(guò)多!等待10秒后再次嘗試")
? ? ? ? ? ? ? ? ? ? sleep(10)
? ? ? ? ? ? ? ? ? ? raise OSError
? ? ? ? ? ? ? ? # 網(wǎng)站服務(wù)不可用
? ? ? ? ? ? ? ? if htm[1] == "!":
? ? ? ? ? ? ? ? ? ? pan = False
? ? ? ? ? ? ? ? ? ? print(gtime()+f"get_skin_thread(id:{id}):網(wǎng)站服務(wù)不可用,等待60秒后再次嘗試")
? ? ? ? ? ? ? ? ? ? sleep(60)
? ? ? ? ? ? ? ? ? ? raise OSError
? ? ? ? ? ? except IndexError:
? ? ? ? ? ? ? ? pass
? ? ? ? ? ? pan = True
? ? ? ? ? ? break
? ? ? ? except OSError:
? ? ? ? ? ? pass
? ? ? ? except Exception:
? ? ? ? ? ? text = text + "\n" + \
? ? ? ? ? ? ? ? gtime()+f"get_skin_thread(id:{id}):網(wǎng)站超時(shí)!嘗試重新連接。(第{to_try+1}次)"
? ? ? ? ? ? print(gtime()+f"get_skin_thread(id:{id}):網(wǎng)站超時(shí)!嘗試重新連接。(第{to_try+1}次)")
? ? ? ? ? ? to_try += 1
? ? ? ? ? ? if to_try > 10: ?# 網(wǎng)站無(wú)響應(yīng)十次,退出
? ? ? ? ? ? ? ? code = "error1"
? ? ? ? ? ? ? ? break
? ? if code == "error1": ?# 網(wǎng)站無(wú)響應(yīng),停止運(yùn)行
? ? ? ? text = text + "\n"+gtime() + error1
? ? ? ? code = "break"
? ? if code == "error2":
? ? ? ? text = text + "\n"+gtime() + error2
? ? ? ? code = "continue" ?# 網(wǎng)站響應(yīng),但無(wú)數(shù)據(jù),跳過(guò)
? ? return htm, text, code
def download(url, path, name): ?# 下載一個(gè)文件
? ? while True:
? ? ? ? try:
? ? ? ? ? ? r = requests.get(url)
? ? ? ? ? ? break
? ? ? ? except ConnectionError:
? ? ? ? ? ? pass
? ? ? ? except Exception:
? ? ? ? ? ? pass
? ? try:
? ? ? ? with open(path, 'wb') as f:
? ? ? ? ? ? f.write(r.content)
? ? except OSError:
? ? ? ? path = "D:\\Desktop\\auto_4\\"+name+"_.png"
? ? ? ? try:
? ? ? ? ? ? with open(path, 'wb') as f:
? ? ? ? ? ? ? ? f.write(r.content)
? ? ? ? except OSError:
? ? ? ? ? ? pass
def get_a_skin(id): ?# 獲取一個(gè)皮膚
? ? global print_pond
? ? global pan
? ? global run
? ? global theard
? ? theard += 1
? ? name = list[i]
? ? print_text = ""
? ? code = ""
? ? print_text = print_text + \
? ? ? ? gtime()+f" -----------------------get_skin_thread(id:{id})_print--------------------------"
? ? print_text = print_text+"\n"+gtime()+f"個(gè)數(shù):{id}"
? ? print_text = print_text+"\n"+gtime()+"玩家名:"+str(name)
? ? url = "https://api.mojang.com/users/profiles/minecraft/"+name ?# 獲取URL
? ? # 請(qǐng)求
? ? html, text, code = html_get(url, header1, id, error2="此皮膚不存在!")
? ? print_text = print_text + text
? ? # 錯(cuò)誤響應(yīng)
? ? if code == "break":
? ? ? ? print_pond.append(print_text)
? ? ? ? run = False
? ? ? ? return
? ? if code == "continue":
? ? ? ? print_pond.append(print_text)
? ? ? ? return
? ? # 錯(cuò)誤響應(yīng)
? ? # 請(qǐng)求
? ? # 獲取URL
? ? uuid = str(re.findall("[0123456789abcdef]{32}", html)[0])
? ? print_text = print_text+"\n"+gtime()+"玩家uuid:"+str(uuid)
? ? header2["path"] = "/session/minecraft/profile/"+uuid
? ? url = "https://sessionserver.mojang.com/session/minecraft/profile/"+uuid
? ? # 獲取URL
? ? # 請(qǐng)求
? ? html, text, code = html_get(url, header2, id)
? ? print_text = print_text + text
? ? # 錯(cuò)誤響應(yīng)
? ? if code == "break":
? ? ? ? print_pond.append(print_text)
? ? ? ? run = False
? ? ? ? return
? ? if code == "continue":
? ? ? ? print_pond.append(print_text)
? ? ? ? return
? ? # 錯(cuò)誤響應(yīng)
? ? # 請(qǐng)求
? ? base = str(re.findall(r'"value" : "[\w=]+"', html)[0]) ?# 提取base64編碼
? ? # base64解碼
? ? base = base[11:len(base)-1]
? ? html = base64.b64decode(base)
? ? html = html.decode('utf-8')
? ? # base64解碼
? ? # 獲取皮膚url
? ? try: ?# 嘗試兩種過(guò)濾方案
? ? ? ? url = re.findall(r'''"SKIN" : {
? "url" : "http://textures.minecraft.net/texture/[0123456789abcdef]+''', html)[0]
? ? except IndexError:
? ? ? ? url = re.findall(r'''"SKIN" : {
? ? ? "url" : "http://textures.minecraft.net/texture/[0123456789abcdef]+''', html)[0]
? ? url = url[26:len(url)]
? ? # 獲取皮膚url
? ? # 保存
? ? path = "D:\\Desktop\\auto_4\\"+name+".png"
? ? print_text = print_text+"\n"+gtime()+"皮膚網(wǎng)址:"+str(url)
? ? print_text = print_text+"\n"+gtime()+"保存地址:"+str(path)
? ? download(url, path, name)
? ? # 保存
? ? code = "OK"
? ? print_pond.append(print_text)
? ? theard -= 1
? ? return
def thread_print():
? ? global print_pond
? ? while run:
? ? ? ? try:
? ? ? ? ? ? print(print_pond[0])
? ? ? ? ? ? print_pond.pop(0)
? ? ? ? ? ? sleep(0.1)
? ? ? ? except IndexError:
? ? ? ? ? ? pass
def thread_stop(): ?# 停止線程函數(shù)
? ? global run
? ? choose = pyautogui.confirm(text="請(qǐng)選擇", title="選擇框", buttons=["停止程序"])
? ? if choose == "停止程序":
? ? ? ? run = False
# 函數(shù)定義
# 打開(kāi)記錄
try:
? ? f = open(r"D:\Desktop\1.txt")
? ? start = int(f.read())
? ? start = start - 2
? ? f.close()
except Exception:
? ? pass
# 打開(kāi)記錄
# 處理名字
for i in range(len(list)):
? ? if list[i][len(list[i])-1] == "\n":
? ? ? ? list[i] = list[i][:len(list[i])-1]
# 處理名字
_thread.start_new_thread(thread_print, tuple()) ?# 啟動(dòng)輸出線程
for i in range(start, len(list)):
? ? while not pan:
? ? ? ? sleep(1)
? ? n = list[i]
? ? p = "D:\\Desktop\\auto_4\\"+n+".png"
? ? if not os.path.isfile(p):
? ? ? ? _thread.start_new_thread(get_a_skin, (i,))
? ? ? ? f = open(r"D:\Desktop\1.txt", "w+") ?# 保存記錄
? ? ? ? print(i, file=f)
? ? ? ? f.close()
? ? ? ? sleep(1.1)
? ? else:
? ? ? ? print(gtime()+f"Main Theard:{n}的皮膚已存在")
? ? if not run:
? ? ? ? break
run = False
name_list.close()
log.close()
-----------------------