Python 一鍵獲取百度網(wǎng)盤提取碼的方法
該 GIF 圖來自于官網(wǎng),文末有給出鏈接。
描述
依托于百度網(wǎng)盤巨大的的云存儲空間,絕大數(shù)人會習(xí)慣性的將一些資料什么的存儲到上面,但是有的私密鏈接需要提取碼,但是讓每個想下載私密資源的人記住每一個提取碼顯然是不現(xiàn)實的。這個時候,云盤萬能鑰匙 誕生了,我們通過安裝相應(yīng)的瀏覽器插件就可以自動獲獲取相應(yīng)鏈接的提取碼。我在 Github 上看了一下,有 Web JS 版的, python 版的貌似還沒有找到,所以我參照了JS 版本和官網(wǎng)的請求接口寫了兩種方式的獲取腳本。
實現(xiàn)
下述兩種方式的具體實現(xiàn)就不做代碼解釋了,思路都是一樣,通過請求接口,拿到數(shù)據(jù),然后返回即可。
v1
""" Author:hippieZhou Date:20190608 Description: Get BaiDuYun shared link's Code """ import argparse import re import requests import json import time VERSION = "VERSION 1.0.0" def checkUrl(url: str) -> str: m1 = re.match( "https?:\/\/pan\.baidu\.com\/s\/1([a-zA-Z0-9_\-]{5,22})", url) m2 = re.match( "https?:\/\/pan\.baidu\.com\/share\/init\?surl=([a-zA-Z0-9_\-]{5,22})", url) if not m1 and not m2: print("參數(shù)不合法") return False else: return True def getKey(url: str) -> bool: if checkUrl(url): try: req = requests.get(f"https://node.pnote.net/public/pan?url={url}") code = req.status_code if code == 200: data = dict(json.loads(req.text)) status = data.get("status", False) if status: return data.get("access_code", "未能查詢到該鏈接的提取碼,可能原因是:該鏈接不需要提取碼或已過期") else: return data.get("messages", "為能查詢到提取碼") elif code == 404: return "不存在該鏈接的記錄" except Exception as e: return f"請求服務(wù)器失敗,錯誤代碼:[code]" def get_parser(): parser = argparse.ArgumentParser() parser.description = "百度網(wǎng)盤提取碼一鍵獲取器" parser.add_argument('urls', metavar="urls", type=str, nargs="*", help='設(shè)置要獲取提取碼的鏈接(多個鏈接請用空格分隔)') parser.add_argument('-v', '--version', action='store_true', help='版本號') return parser def command_line_runner(): parser = get_parser() args = vars(parser.parse_args()) if args['version']: print(VERSION) return s_time = time.time() if len(args['urls']) > 1: for item in args["urls"][1:]: print(f"{item}:\r\n\t{getKey(item)}") e_time = time.time() print(f"\n\n操作完畢,總耗時:{e_time-s_time} 秒") def main(): command_line_runner() if __name__ == "__main__": main()
運行效果如下圖所示:
v2
""" Author:hippieZhou Date:20190608 Description: Get BaiDuYun shared link's Code """ import argparse import time import re import requests from datetime import datetime import json accessKey = "4fxNbkKKJX2pAm3b8AEu2zT5d2MbqGbD" clientVersion = "web-client" def getPid(url: str) -> str: matches = re.match( "https?:\/\/pan\.baidu\.com\/s\/1([a-zA-Z0-9_\-]{5,22})", url) return matches[1] if matches else None def getUuid(pid: str) -> str: return f"BDY-{pid}" def getKey(url: str) -> str: pid = getPid(url) uuid = getUuid(pid) headers = { "type": "GET", "data": '', "dataType": "json" } url = f"http://ypsuperkey.meek.com.cn/api/items/{uuid}?access_key={accessKey}&client_version={clientVersion}&{datetime.utcnow()}" try: req = requests.get(url, headers=headers) code = req.status_code if code == 200: data = json.loads(req.text) accessCode = data.get("access_code", None) return "沒找到提取密碼,o(╥﹏╥)o" if (accessCode == "undefined" or accessCode == None or accessCode == "") else accessCode elif code == 400: return " 服務(wù)器不理解請求的語法" elif code == 404: return "不存在該鏈接的記錄" else: return f"請求服務(wù)器失敗,錯誤代碼:[code]" except Exception as e: return e def get_parser(): parser = argparse.ArgumentParser() parser.description = "百度網(wǎng)盤提取碼一鍵獲取器" parser.add_argument('urls', metavar="urls", type=str, nargs="*", help='設(shè)置要獲取提取碼的鏈接(多個鏈接請用空格分隔)') parser.add_argument('-v', '--version', action='store_true', help='版本號') return parser def command_line_runner(): parser = get_parser() args = vars(parser.parse_args()) if args['version']: print(VERSION) return s_time = time.time() if len(args['urls']) > 1: for item in args["urls"][1:]: print(f"{item}:\r\n\t{getKey(item)}") e_time = time.time() print(f"\n\n操作完畢,總耗時:{e_time-s_time} 秒") def main(): command_line_runner() if __name__ == "__main__": main()
運行效果如下圖所示:
總結(jié)
v1 版本和 v2 版本是通過請求不同的接口方式來實現(xiàn)的, v2 接口的數(shù)據(jù)要相對更準確一些。具體可查閱具體的代碼實現(xiàn)。
如果你覺得上述代碼不錯的話,歡迎訪問對應(yīng)的倉庫地址: baidupankey 進行 star 、fork 和 follow。
相關(guān)參考
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python使用Pandas庫實現(xiàn)MySQL數(shù)據(jù)庫的讀寫
這篇文章主要介紹了Python使用Pandas庫實現(xiàn)MySQL數(shù)據(jù)庫的讀寫 ,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07python中的print()函數(shù)end=' '的使用及說明
這篇文章主要介紹了python中的print()函數(shù)end=' '的使用及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02python3實現(xiàn)二叉樹的遍歷與遞歸算法解析(小結(jié))
這篇文章主要介紹了python3實現(xiàn)二叉樹的遍歷與遞歸算法解析(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07