python自动化之转存资源到自己网盘中

AlgoCraft
• 阅读 92
如果有帮助到您还请动动手帮忙点赞,关注,评论转发,感谢啦!💕💕💕😘😘😘 本文由Butterfly一键发布工具发布

python自动化之转存资源到自己网盘中

使用selenium库和bypy库

代码入下

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import time
import re

from bypy import ByPy

# 获取一个bypy对象,封装了所有百度云文件操作的方法
bp = ByPy()

driver = webdriver.Chrome()  # 括号参数:executable_path="driver路径",可配置浏览器驱动的目录加入了环境变量,就不用括号里的参数了


# def login():
#     # 初始化WebDriver
#     driver = webdriver.Chrome()
#
#     # 打开登录页面
#     driver.get(
#         "https://pan.baidu.com/login?redirecturl=https://pan.baidu.com/s/18BSsXsCKUfpEumKUMT8mTA")
#
#     time.sleep(4)  # 等待到完全加载后再找元素
#
#     driver.find_element(By.CLASS_NAME, "bd-login-button__wrapper").click()
#     time.sleep(3)  # 等待到完全加载后再找元素
#
#     # 填写用户名和密码(假设已经找到了对应的输入框)
#     username_input = driver.find_element(By.ID, "TANGRAM__PSP_11__userName")
#     print(username_input, 'username_input')
#
#     password_input = driver.find_element(By.ID, "TANGRAM__PSP_11__password")
#     username_input.send_keys("xxx@163.com")
#     password_input.send_keys("xxx")
#
#     # 点击复选框
#     checkbox = driver.find_element(By.ID, "TANGRAM__PSP_11__isAgree")
#     checkbox.click()
#     time.sleep(1)  # 等待到完全加载后再找元素
#     # 点击登录按钮
#     login_button = driver.find_element(By.ID, "TANGRAM__PSP_11__submit")
#     login_button.click()
#
#     # 等待重定向或检查登录状态
#     try:
#         # 显式等待,例如等待某个元素在回调页面上出现
#         WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.CLASS_NAME, "pointer-events-all")))
#         print("成功重定向到回调页面")
#     except Exception as e:
#         print("重定向失败或超时:", e)
#         # 在这里你可以添加逻辑来处理失败的情况,例如手动触发重定向或重新登录
#
#     # 检查当前URL
#     current_url = driver.current_url
#     print("当前URL:", current_url)
#     time.sleep(10)  # 等待到完全加载后再找元素
#     main()


def main(projectArr, item):
    try:
        driver.get(projectArr[item]['link'])  # 先建立链接
        elem = driver.find_element(By.ID, "accessCode")
        elem.send_keys(projectArr[item]['num'])
        # time.sleep(4)  # 等待到完全加载后再找元素
        driver.find_element(By.ID, 'submitBtn').click()
        # elem.send_keys(Keys.RETURN)  # 输入回车
        time.sleep(1)  # 等待到完全加载后再找元素
        nowurl = driver.current_url
        # 先判断是否有type文件夹,如果没有则先创建
        print(nowurl, 'nowurl')
        # sreach_window = driver.current_window_handle
        try:
            driver.find_element(By.CLASS_NAME, 'tools-share-save-hb').click()
            time.sleep(3)
            driver.find_element(By.XPATH, '//*[@id="fileTreeDialog"]/div[2]/div/ul/li/ul/li[5]').click()
            time.sleep(1)
            driver.find_element(By.XPATH, '//*[@id="fileTreeDialog"]/div[2]/div/ul/li/ul/li[5]/ul/li').click()
            time.sleep(1)

            # if driver.find_element(By.LINK_TEXT, projectArr[item]['type']):
            #     driver.find_element(By.LINK_TEXT, projectArr[item]['type']).click()
            # else:
            # driver.find_element(By.PARTIAL_LINK_TEXT, projectArr[item]['type']).click()
            driver.find_element(By.XPATH,
                                '//*[@id="fileTreeDialog"]/div[2]/div/ul/li/ul/li[5]/ul/li/ul/li[' + str(projectArr[item]['index']) + ']').click()
            # driver.find_element(By.XPATH, '//*[@id="fileTreeDialog"]/div[2]/div/ul/li/ul/li[5]/ul/li/ul/li').click()
            time.sleep(1)
            driver.find_element(By.CLASS_NAME, 'g-button-blue-large').click()  # 找到确认按钮点击
            WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CLASS_NAME, "info-section-title")))
            print('success')
            item = item + 1
            main(projectArr, item)

        except Exception as e:
            print(e, 'eeeee')
            username_input = driver.find_element(By.ID, "TANGRAM__PSP_11__userName")
            username_input.send_keys("xxx@163.com") # 网盘账号
            time.sleep(3)
            password_input = driver.find_element(By.ID, "TANGRAM__PSP_11__password")
            password_input.send_keys("xxx") # 网盘密码
            #     # 点击登录按钮
            login_button = driver.find_element(By.ID, "TANGRAM__PSP_11__submit")
            login_button.click()
            time.sleep(30)
            driver.find_element(By.XPATH, '//*[@id="fileTreeDialog"]/div[2]/div/ul/li/ul/li[5]').click()
            time.sleep(1)
            driver.find_element(By.XPATH, '//*[@id="fileTreeDialog"]/div[2]/div/ul/li/ul/li[5]/ul/li').click()
            time.sleep(1)

            # if driver.find_element(By.LINK_TEXT, projectArr[item]['type']):
            #     driver.find_element(By.LINK_TEXT, projectArr[item]['type']).click()
            # else:
            # driver.find_element(By.PARTIAL_LINK_TEXT, projectArr[item]['type']).click()
            # time.sleep(4) # 留时间保证文件夹创建成功
            print(projectArr[item]['type'], 'tyweeeeeee-')
            driver.find_element(By.XPATH,
                                '//*[@id="fileTreeDialog"]/div[2]/div/ul/li/ul/li[5]/ul/li/ul/li[' + str(projectArr[item]['index']) + ']').click()
            time.sleep(1)
            driver.find_element(By.CLASS_NAME, 'g-button-blue-large').click()  # 找到确认按钮点击
            WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CLASS_NAME, "info-section-title")))
            print('Exception')
            item = item + 1
            main(projectArr, item)

    except Exception as e:
        item = item + 1
        main(projectArr, item)


if __name__ == "__main__":
    with open('data/资源.txt', 'r', encoding='utf-8') as file:
        # 读取文件内容
        projectArr = eval(file.read())
        # 打印内容
        print(projectArr, 'rrr')
        main(projectArr, 0)

整理的资源txt如下格式

[
    {
        'link': 'https://pan.baidu.com/s/12OQ_sa1-SiWbSH2qt4aEeg',
        'num': 'rgbk',
        'type': '赚钱指导说明书.pdf',
        'index': 1
    }
]

本文章主打的就是没废话,如果帮助到你,还请一键三连。

如果有帮助到您还请动动手帮忙点赞,关注,评论转发,感谢啦!💕💕💕😘😘😘 本文由Butterfly一键发布工具发布
点赞
收藏
评论区
推荐文章
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
4年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Wesley13 Wesley13
4年前
java web 中的转发和重定向路径问题
    假设应用程序的contextPath为/ctx,在http://localhost:8080/ctx/a/b资源中,我们转发和重定向到http://localhost:8080/ctx/x/y资源,分别应该怎么写?    转发,是在同一个应用程序中,请求从由资源A处理,到由资源B处理。资源A和资源B属于同一个应用程序
Stella981 Stella981
4年前
React Hooks实现异步请求实例—useReducer、useContext和useEffect代替Redux方案
<blockquote本文是学习了2018年新鲜出炉的ReactHooks提案之后,针对<strong异步请求数据</strong写的一个案例。注意,本文假设了:<br1.你已经初步了解<codehooks</code的含义了,如果不了解还请移步<ahref"https://reactjs.org/docs/hooksintro.html
Wesley13 Wesley13
4年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Immerse Immerse
9个月前
做Docx预览,一定要做这个神库!!
Hey,我是沉浸式趣谈本文首发于【沉浸式趣谈】,我的个人博客https://yaolifeng.com也同步更新。转载请在文章开头注明出处和版权信息。如果本文对您有所帮助,请点赞、评论、转发,支持一下,谢谢!只需几行代码,你就能在浏览器中完美预览Word文
AlgoCraft
AlgoCraft
Lv1
纵然万劫不复纵然相思入骨我也待你眉眼如初岁月如故.
文章
8
粉丝
0
获赞
0