百度网盘批量保存
本帖最后由 Arcticlyc 于 2023-6-12 12:15 编辑前言
前段时间Minecraft的买一赠一活动已经出来了,我也用自己基岩版的账号白嫖了java版Minecraft,这两天想起来很久以前看的一个实况解说,就去网上找了他分享的存档和资源包,但是下载下来发现是很多个网盘链接存在一个txt文件中,我保存几个之后就不想点了,一是太无聊,而是怕自己看花眼了。所以写了个批量保存百度网盘资源的脚本。
脚本简介
通过解析百度网盘保存链接,只需要要保存的网盘资源链接,用户的cookie和需要保存到的路径,文件名只是便于控制台查看结果,并无实际作用。
上传模式分为单个文件和多个文件(txt文件),批量保存可选择多文件保存。
脚本资源
链接: https://pan.baidu.com/s/19fEGBA4quuT2R7CCuULY5w?pwd=84up 提取码: 84up 复制这段内容后打开百度网盘手机App,操作更方便哦
代码部分
# -*- encoding: utf-8 -*-
'''
@file: BaiduPan.py
@Time: 2022/08/24 11:54:57
@AuThor: Arctic
@version: 1.0
@Contact: jc2150828@gmail.com
@desc:
'''
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)
shareid = re.findall(r'shareid:"(\d+)"', res.text)
app_id = re.findall(r'app_id":"(\d+)"', res.text)
fs_id = re.findall(r'fs_id":(\d+),', res.text)
bdstoken = re.findall(r"bdstoken:'(\w+)'", res.text)
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']))
logid = base64.b64encode(re.findall(r'BAIDUID=(.+?);', headers['Cookie']).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 =
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) Arcticlyc 发表于 2023-1-17 19:06
名称 链接
如上格式,每行一个链接
File "G:\python\BaiduPan\BaiduPan.py", line 135, in <module>
txt_upload(file, cookie, path)
会报错
File "G:\python\BaiduPan\BaiduPan.py", line 114, in txt_upload
print(name + pan.upload(url, path))
File "G:\python\BaiduPan\BaiduPan.py", line 65, in upload
print(self.get_info(uri))
File "G:\python\BaiduPan\BaiduPan.py", line 32, in get_info
app_id = re.findall(r'app_id":"(\d+)"', res.text)
IndexError: list index out of range Lambor_G 发表于 2023-1-18 09:43
File "G:\python\BaiduPan\BaiduPan.py", line 135, in
txt_upload(file, cookie, path)
会报错
...
可能规则更新了{:1_924:} 感谢楼主分享,支持一下! 怎么的呀 老哥 FM107.9 发表于 2022-8-29 18:28
怎么的呀 老哥
什么怎么的? 怎么使用呢? 大佬牛啊??先收藏了再说 没啥用感谢分享 怎么用啊,不会用。 想问一下txt文档的格式是怎样的 Lambor_G 发表于 2023-1-17 18:14
想问一下txt文档的格式是怎样的
名称 链接
如上格式,每行一个链接
名称无实际意义,随意
链接为百度网盘的分享链接
页:
[1]
2