鱼C论坛

 找回密码
 立即注册
查看: 2365|回复: 0

[作品展示] python + chromedriver 实现自动批量保存百度盘分享

[复制链接]
发表于 2021-3-28 16:49:00 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x

python + chromedriver实现自动批量保存百度盘分享

已将提取百度云分享的Python代码封装成了类BaiduPanExtractor


  1. from selenium import webdriver
  2. import threading
  3. import time


  4. class BaiduPanExtractor:
  5.     def __init__(self):
  6.         self.browser = None
  7.         self.password = None
  8.         self.account = None
  9.         self.executable_path = None
  10.         self.login_status = False   # 记录登录状态

  11.     def open_browser(self, executable_path="chromedriver"):
  12.         '''
  13.             :param executable_path 指定chromedriver的路径
  14.         '''
  15.         try:
  16.             self.browser = webdriver.Chrome(executable_path=executable_path)
  17.         except Exception:
  18.             print("请指定Chromedriver.exe的路径")

  19.     #   超过2s未检查出来指定元素,默认为该网页无该元素,抛出异常
  20.     def decorator_exception(fun):
  21.         def wrap(*args):
  22.             sleep_time = 0
  23.             while True:
  24.                 time.sleep(1)
  25.                 try:
  26.                     return fun(*args)
  27.                 except Exception as reason:
  28.                     sleep_time = sleep_time + 1
  29.                     if sleep_time >= 2:
  30.                         raise reason
  31.         return wrap

  32.     #   打开网页百度云盘登录连接
  33.     def open_baidu_pan(self):
  34.         self.browser.get("http://pan.baidu.com")

  35.     #   检查登录状态
  36.     def check_login_status(self):
  37.         while not self.login_status:
  38.             if "https://pan.baidu.com/disk/home?" in self.browser.current_url:
  39.                 self.login_status = True

  40.     #   点击”保存到网盘“按钮
  41.     @decorator_exception
  42.     def click_save2disk(self):
  43.         self.browser.find_element_by_class_name("tools-share-save-hb").click()

  44.     #   点击”新建文件夹“按钮
  45.     @decorator_exception
  46.     def click_create_new_folder(self):
  47.         self.browser.find_element_by_xpath("//a[@data-button-id='b17']").click()

  48.     #   点击分享页面“确认”按钮
  49.     @decorator_exception
  50.     def click_confirm(self):
  51.         self.browser.find_element_by_xpath("//a[@node-type='confirm']").click()

  52.     #   点击分享页面“✔”按钮
  53.     @decorator_exception
  54.     def click_sure(self):
  55.         self.browser.find_element_by_class_name("sure").click()

  56.     #   将文件保存到“file_path”目录下
  57.     def save(self, file_path):
  58.         self.locate_folder(file_path)
  59.         self.click_confirm()

  60.     #   检查”save_path“路径是否存在
  61.     def is_path_exist(self, save_path):
  62.         return self.locate_folder(save_path)

  63.     #   定位“file_name”文件
  64.     def locate_folder(self, file_name):
  65.         pattern = "//span[@node-path='{}']/../..".format(file_name)
  66.         sleep_time = 0
  67.         while True:
  68.             time.sleep(1)
  69.             try:
  70.                 self.browser.find_element_by_xpath(pattern).click()
  71.                 return True
  72.             except Exception:
  73.                 sleep_time = sleep_time + 1  # 定位文件时,如果2s内未找到该文件,视为目录中没有该文件
  74.                 if sleep_time >= 2:
  75.                     return False

  76.     #   新建路径未file_path的文件夹
  77.     @decorator_exception
  78.     def create_new_folder(self, file_path):
  79.         self.click_create_new_folder()  # 点击新建文件夹按钮
  80.         temp = self.browser.find_element_by_class_name("shareFolderInput")  # 定位文件名输入框
  81.         temp.clear()  # 清空输入框
  82.         temp.send_keys(file_path[file_path.rfind("/"):])  # 填写文件名
  83.         self.click_sure()  # 确认填写

  84.     #   提交用户名和登录密码
  85.     @decorator_exception
  86.     def submit_login_form(self):
  87.         username = self.browser.find_element_by_class_name("pass-text-input-userName")
  88.         password = self.browser.find_element_by_class_name("pass-text-input-password")
  89.         username.clear()
  90.         password.clear()
  91.         username.send_keys(self.account)
  92.         password.send_keys(self.password)
  93.         self.browser.find_element_by_id("TANGRAM__PSP_4__submit").click()
  94.         while not self.login_status:
  95.             pass

  96.     #   打开分享界面
  97.     def extract_share(self, share_url, share_url_code):
  98.         self.browser.get(share_url)
  99.         self.browser.find_element_by_id("accessCode").send_keys(share_url_code)
  100.         self.browser.find_element_by_class_name("submit-a").click()
  101.         current_url = self.browser.current_url
  102.         while current_url == self.browser.current_url:
  103.             pass

  104.     #   将文件保存在save_path路径下
  105.     def save_file(self, save_path):
  106.         catalog_list = save_path.split("/")
  107.         catalog_list.remove("")
  108.         length = len(catalog_list)
  109.         file_path = ""
  110.         for i in range(length):
  111.             file_path = file_path + "/" + catalog_list[i]
  112.             if not self.is_path_exist(file_path):
  113.                 self.create_new_folder(file_path)
  114.             if file_path == save_path:
  115.                 self.save(save_path)

  116.     #   登录百度云盘
  117.     def login(self,account=None,password=None):
  118.         '''
  119.             :param account 百度云的账户
  120.             :param password 百度云账户密码
  121.             若用户名或密码为空,则打开浏览器手动登录
  122.             若用户名或密码不为空,则打开浏览器自动提交用户名和密码,(百度云可能会有验证,这时需要手动验证)
  123.         '''
  124.         self.account = account
  125.         self.password = password
  126.         self.open_baidu_pan()
  127.         threading.Thread(target=self.check_login_status).start()
  128.         while not self.login_status:
  129.             if self.account and self.password:
  130.                 self.submit_login_form()

  131.     def check_share_type(self):
  132.         if "#list" in self.browser.current_url:
  133.             return "FOLDER"
  134.         return "FILE"

  135.     #   点击“文件名”按钮
  136.     @decorator_exception
  137.     def click_file_name_checkbox(self):
  138.         self.browser.find_element_by_xpath("//div[@class='QxJxtg']/div/ul/li/div").click()

  139.     #   将链接为share_url的文件保存在save_path路径下
  140.     def save2baidu_pan_disk(self, share_url, share_url_code, save_path):
  141.         '''
  142.             :param share_url 分享连接
  143.             :param share_url_code 分享连接提取码
  144.             :param save_path 资源保存位置 默认为根路径
  145.         '''
  146.         self.extract_share(share_url, share_url_code)

  147.         if self.check_share_type() == "FOLDER":
  148.             self.click_file_name_checkbox()

  149.         self.click_save2disk()
  150.         self.save_file(save_path)
复制代码



使用方法:

  1. chromedriver_executable = "指定你的chromedriver的路径"
  2. account = "你的百度云"
  3. password = "你的百度云密码"

  4. baidupan_extractor = BaiduPanExtractor()
  5. baidupan_extractor.open_browser(chromedriver_executable)
  6. baidupan_extractor.login(account = account,password=password)
  7. baidupan_extractor.save2baidu_pan_disk(share_url="百度云分享链接", share_url_code="百度云分享链接提取码",save_path="指定文件保存路径")
复制代码


账户和密码可以不用输入,运行代码后会打开浏览器,弹出百度网盘登录界面,可在该界面输入账户和密码登录并验证,之后浏览器会自动进入到你填写的百度云分享连接,并提取文件

写脚本的原因是,在github上发现某大神分享了很关于计算机知识的电子书

book.png

一个个手动点击保存过于繁琐于是写了个脚本将该页的链接和提取码爬取了下来并配合chromedriver实现模拟手动点击输入提取码,点击提取文件等过程。



小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-26 00:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表