菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

将标题为:使用Python脚本解析并下载123网盘文件

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:python教程 Python脚本 网盘解析 123网盘

123网盘解析下载Python脚本可以使用第三方库`requests`和`BeautifulSoup`实现。首先,安装这两个库: ```bash pip install requests pip install beautifulsoup4 ``` 然后,编写Python脚本: ```python import requests from bs4 import BeautifulSoup def download_file(url, save_path): response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') file_id = soup.find('input', {'id': 'file_id'})['value'] download_url = f'http://123pan.com/file/{file_id}' with open(save_path, 'wb') as f: f.write(requests.get(download_url).content) if __name

importrequestsimportreimporttkinterastkfromioimportBytesIOfromurllib.requestimporturlopenimportbase64importjsonclassForm1(tk.Tk):def__init__(self):super().__init__()self.title("123Pan下载工具")self.geometry("400x300")self.label1=tk.Label(self,text="请输入123盘的网址:")self.label1.pack()self.entry=tk.Entry(self,width=50)self.entry.pack()self.button1=tk.Button(self,text="开始下载",command=self.download)self.button1.pack()self.label2=tk.Label(self,text="提取下载链接:")self.label2.pack()self.label3=tk.Label(self,text="")self.label3.pack()self.textbox=tk.Text(self,height=5,width=50)self.textbox.pack()self.button2=tk.Button(self,text="清除输入",command=self.clear_input)self.button2.pack()defdownload(self):url=self.entry.get()share_id,share_Code=self.extract_id_and_code(url)ifshare_id:self.get_info(share_id,share_Code)else:tk.messagebox.showerror("错误","没有找到123盘的网址!")defextract_id_and_code(self,url):share_id=re.search(r"(?<=/s/)[^./]+",url)share_Code=re.search(r"提取码:(\w+)",url)ifshare_id:share_id=share_id.group(0)ifshare_Code:share_Code=share_Code.group(1)ifshare_Code=="":self.label3.config(text="")else:self.label3.config(text="提取码:"+share_Code)returnshare_id,share_CodereturnNone,Nonedefget_info(self,share_id,share_Code):headers={"User-Agent":"Mozilla/5.0(WindowsNT6.1;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/109.0.0.0Safari/537.36Edg/109.0.1518.100"}request_url=f"https://www.123pan.com/b/api/share/get?limit=100&next=1&orderBy=share_id&orderDirection=desc&shareKey={share_id}&SharePwd={share_Code}&ParentFileId=0&Page=1"response=requests.get(request_url,headers=headers)ifresponse.status_code==200:response_text=response.textself.parse_get_info(response_text,share_id)else:tk.messagebox.showerror("错误","获取用户信息失败")defparse_get_info(self,response_text,share_id):data=json.loads(response_text)code=data["code"]ifcode==0:info_list=data["data"]["InfoList"]ifinfo_list:info=info_list[0]ifinfo["Type"]==0:file_id=info["FileId"]size=info["Size"]s3_key_flag=info["S3KeyFlag"]etag=info["Etag"]self.combined_data(share_id,file_id,size,s3_key_flag,etag)else:self.textbox.delete(1.0,tk.END)self.textbox.insert(tk.END,"错误:软件目前不支持文件夹解析。")else:message=data["message"]tk.messagebox.showerror("错误",f"错误代码{code}//{message}")defcombined_data(self,share_id,file_id,size,s3_key_flag,etag):headers={"User-Agent":"Mozilla/5.0(WindowsNT6.1;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/109.0.0.0Safari/537.36Edg/109.0.1518.100","Content-Type":"application/json;charset=UTF-8"}url="https://www.123pan.com/b/api/share/download/info"data={"ShareKey":share_id,"FileID":file_id,"S3keyFlag":s3_key_flag,"Size":size,"Etag":etag}response=requests.post(url,headers=headers,json=data)ifresponse.status_code==200:response_text=response.textself.parse_download_url(response_text)else:tk.messagebox.showerror("错误","获取下载链接失败")defparse_download_url(self,response_text):data=json.loads(response_text)code=data["code"]ifcode==0:download_url=data["data"]["DownloadURL"]decoded_url=self.extract_download_url(download_url)self.get_xz_url(decoded_url)else:message=data["message"]tk.messagebox.showerror("错误",f"错误代码{code}//{message}")defextract_download_url(self,url):params_match=re.search(r"params=([^&]+)",url)ifparams_match:params_value=params_match.group(1)decoded_bytes=base64.b64decode(params_value)decoded_string=decoded_bytes.decode("utf-8")if"auto_redirect"notindecoded_string:if"?"indecoded_string:decoded_string+="&auto_redirect=0"else:decoded_string+="?auto_redirect=0"returndecoded_stringelse:tk.messagebox.showerror("错误","提取DownloadURL失败")defget_xz_url(self,request_url):headers={"User-Agent":"Mozilla/5.0(WindowsNT6.1;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/109.0.0.0Safari/537.36Edg/109.0.1518.100"}response=requests.get(request_url,headers=headers)ifresponse.status_code==200:response_text=response.textself.parse_xz_url(response_text)else:tk.messagebox.showerror("错误","获取直链失败")defparse_xz_url(self,response_text):data=json.loads(response_text)code=data["code"]ifcode==0:redirect_url=data["data"]["redirect_url"]self.textbox.delete(1.0,tk.END)self.textbox.insert(tk.END,redirect_url)else:message=data["message"]tk.messagebox.showerror("错误",f"错误代码{code}//{message}")defclear_input(self):self.entry.delete(0,tk.END)self.textbox.delete(1.0,tk.END)if__name__=="__main__":form=Form1()form.mainloop()


评论
建站知识
建站知识
使用技巧
调试安装
运营推广