python教程

批量转存百度网盘分享资源Python脚本

我的站长站 2022-09-29 人阅读

前言

批量转存百度网盘分享资源Python脚本,解封双手,无需一个个链接的点击保存。
通过解析百度网盘保存链接,只需要要保存的网盘资源链接,用户的Cookie和需要保存到的路径,文件名只是便于控制台查看结果,并无实际作用。

上传模式分为单个文件和多个文件(txt文件),批量保存可选择多文件保存。

python脚本代码

# -*- encoding: utf-8 -*-
'''
[url=home.php?mod=space&uid=267492]@file[/url]    :   BaiduPan.py
[url=home.php?mod=space&uid=238618]@Time[/url]    :   2022/08/24 11:54:57
[url=home.php?mod=space&uid=686208]@AuThor[/url]  :   Arctic
[url=home.php?mod=space&uid=1248337]@version[/url] :   1.0
@Contact :   [email]784060033@qq.com[/email]
[url=home.php?mod=space&uid=1781681]@desc[/url]    :   
'''
 
 
import math
import random
import re
import requests
import base64
from urllib.parse import unquote
 
class BaiduPan:
    def __init__(self, cookie) -> None:
        self.headers = {
            'Host': 'pan.baidu.com',
            'Cookie': cookie,
            'User-Agent': 'Mozilla/5.0'
        }
 
    def get_info(self, url):
        res = requests.get(url, headers=self.headers)
 
        share_uk = re.findall(r'share_uk:"(d+)"', res.text)[0]
        shareid = re.findall(r'shareid:"(d+)"', res.text)[0]
        app_id = re.findall(r'app_id":"(d+)"', res.text)[0]
        fs_id = re.findall(r'fs_id":(d+),', res.text)[0]
        bdstoken = re.findall(r"bdstoken:'(w+)'", res.text)[0]
        return (share_uk, shareid, app_id, fs_id, bdstoken)
 
    def getDpLogId(self, uk=None):
        def getRandomInt(num):
            return str(math.floor((random.random() * 9 + 1) * math.pow(10, num - 1)))
        def validateUk(uk):
            return len(uk + '') == 10
        def prefixInteger(num, length):
            return ''.join(['0' for _ in range(length - len(num))]) + num
        def getCountId(countid=30):
            if countid < 9999:
                countid += 1
            else:
                countid = 0
            return prefixInteger(str(countid), 4)
 
        client = ''
 
        userid = '00' + getRandomInt(8)
        sessionid = getRandomInt(6)
 
        if uk and validateUk(uk):
            userid = uk
        return client + sessionid + userid + getCountId()
 
    def upload(self, uri, path='/我的资源'):
        url = 'https://pan.baidu.com/share/transfer'
 
        headers = self.headers.copy()
        headers['Referer'] = uri
 
        share_uk, shareid, app_id, fs_id, bdstoken = self.get_info(uri)
        sekey = unquote(re.findall(r'BDCLND=(.+?);', headers['Cookie'])[0])
        logid = base64.b64encode(re.findall(r'BAIDUID=(.+?);', headers['Cookie'])[0].encode()).decode()
        dplogid = self.getDpLogId()
        params = {
            'shareid': shareid,
            'from': share_uk,
            'sekey': sekey,
            'channel': 'chunlei',
            'web': '1',
            'app_id': app_id,
            'bdstoken': bdstoken,
            'logid': logid,
            'clienttype': '0',
            'dp-logid': dplogid,
        }
 
        data = {
            'fsidlist': f'[{fs_id}]',
            'path': path
        }
 
        resp = requests.post(url, headers=headers, params=params, data=data).json()
        # pprint.pp(resp)
        if resp['errno'] == 0:
            return '保存网盘成功。'
        elif resp['errno'] == 4:
            return '已经保存过了。'
        elif resp['errno'] == 2:
            return '保存时出现未知错误。'
        else:
            return resp['show_msg']
 
# 单个文件保存
def sing_upload(name, url, cookie, path='/我的资源'):
    pan = BaiduPan(cookie)
    print(name + pan.upload(url, path))
 
# 多个文件保存
def txt_upload(file, cookie, path='/我的资源'):
    with open(file, 'r', encoding='utf8') as f:
        datas = [data.strip().split() for data in f.readlines() if data.strip()]
 
    pan = BaiduPan(cookie)
 
    for data in datas:
        name, url = data
        print(name + pan.upload(url, path))
 
if __name__ == '__main__':
    print('***百度网盘批量保存***')
    print('n请将百度账号cookie复制到cookie.txt文件中')
    with open('cookie.txt', 'r', encoding='utf8') as f:
        cookie = f.read()
 
    mode = input('n1. 单个文件保存n2. 多个文件保存(txt文件)n选择模式:')
    if mode == '1':
        name = input('输入文件名称:')
        url = input('输入网盘链接:')
        path = input('输入保存路径(不输入默认保存至 /我的资源):')
        if not path: 
            path = '/我的资源'
        sing_upload(name, url, cookie, path)
    elif mode == '2':
        file = input('输入txt文件路径:')
        path = input('输入保存路径(不输入默认保存至 /我的资源):')
        if not path: 
            path = '/我的资源'
        txt_upload(file, cookie, path)

完整示例脚本下载:https://pan.baidu.com/s/19fEGBA4quuT2R7CCuULY5w?pwd=84up

相关专题
百度网盘
百度网盘
2020-12-11 722

百度网盘是目前用户最多的网盘,本站全部资源也均在百度网盘,为了方便本站用户使用百度网盘,特意收藏收藏整理了关于百度网盘下载专题,包含各种百度网盘破解版,百...

相关推荐
  • Python脚本
  • 监测腾讯云轻量服务器流量超标关机python脚本

    脚本介绍一款监测腾讯云轻量应用服务器流量包使用情况,并根据配置进行警告和关机的Python脚本。GitHub:https://github.com/XiaoXinYo/Tencent_Cloud_LightHouse_Server_Guardian脚本功能仅用于轻量级服务器1.自动检测流量包剩余,可设置使用比2.自动关...

    python教程 75 1年前
  • Python无需认证QQ扫码登录脚本

    无需认证QQ扫码登录脚本python脚本,盗用JD的QQ登录,也可以改成其他网址。无需自己注册腾讯开发者,无需自己有一套网址去申请应用Get_QQ返回QQ号,也可以获取到QQ头像、好友等其他信息,请勿用于非法行为import requestsimport timefrom PIL import Imagedef...

    python教程 291 2年前
  • 最新python织梦dedecms远程执行脚本

    织梦CMS是使用最多的CMS之 一,但是漏洞也非常多。分享一款python写的织梦远程文件包含漏洞。修复此漏洞方法,请见文章底部。织梦CMS漏洞代码#! /usr/bin/env python#coding=utf-8#Joseph(小续)import requestsimport sysimport redef main():try:url="...

    服务器配置 282 4年前
最新更新