当前位置:   article > 正文

实现百度网盘分享保存到自己的网盘_java 转存百度链接

java 转存百度链接

目录

前言

简介

地址链接

功能

实现百度网盘分享保存到自己的网盘

查看源码的方式出现http地址

java实现解析地址保存到数据库

Python webdriver.Chrome()的使用

Python实现自动分享


前言

本文可以学习了解java file 类操作和python 自动化测试,通过Python webdriver.Chrome()实现百度网盘自动点击分享地址和保存到我的网盘,供个人学习使用,在网上看过很多类似的文章,缺少解析url地址和python版本说明,特此总结

简介

地址链接

这是博主【拓跋阿秀】百度分享的计算机书籍

README.md · forthespada/CS-Books - Gitee.com

功能

实现百度网盘分享保存到自己的网盘

步骤:汇总

1、查看源码的方式出现http地址

2、java实现解析地址保存到数据库

3、Python webdriver.Chrome() 【谷歌驱动】的使用

4、Python实现自动分享

查看源码的方式出现http地址

####  00、C语言
​
- 《C程序设计语言(第二版)》 [百度云链接](https://gitee.com/link?XXX) 提取码:2and
- 《C Primer Plus 中英版》 [百度云链接](https://gitee.com/link?XXX) 提取码:2dox

​

安装typora,下载分享地址链接 点击左下角查看源码或者【ctrl】

将带http地址保存到桌面【C:\Users\Administrator\Desktop\temp.txt】

java实现解析地址保存到数据库

FileUtils.java

  1. package com.bridge.file;
  2. import com.bridge.NetDiskUrl;
  3. import com.bridge.jdbc.JDBCUtils;
  4. import org.springframework.util.StringUtils;
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileInputStream;
  8. import java.io.InputStreamReader;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. public class FileUtils {
  14.    private static String title = "";
  15.    private static String subTitle = "";
  16.    public static void readText() throws Exception {
  17.        File file = new File("C:\\Users\\Administrator\\Desktop\\temp.txt");
  18.        FileInputStream fis = new FileInputStream(file);
  19.        //Construct BufferedReader from InputStreamReader
  20.        BufferedReader br = new BufferedReader(new InputStreamReader(fis));
  21.        String line = null;
  22.        List<NetDiskUrl> list = new ArrayList<>();
  23.        while ((line = br.readLine()) != null) {
  24.            NetDiskUrl netDiskUrl = extractUrls(line);
  25.            if (netDiskUrl != null) {
  26.                list.add(netDiskUrl);
  27. //               System.out.println(netDiskUrl.toString());
  28.           }
  29.       }
  30.        br.close();
  31.        JDBCUtils.batchCommitInsert(list);
  32.   }
  33.    public static NetDiskUrl extractUrls(String input) {
  34.        if (!StringUtils.hasText(input)) {
  35.            return null;
  36.       }
  37.        input = input.trim();
  38.        if (input.startsWith("####") && !input.startsWith("#####")) {
  39.            if (input.contains("、")) {
  40.                String[] split = input.split("、");
  41.                title = split[1];
  42.                subTitle = "";
  43.           }
  44.       }
  45.        if (input.startsWith("#####")) {
  46.            if (input.contains("、")) {
  47.                String[] split = input.split("、");
  48.                subTitle = split[1];
  49.           }
  50.       }
  51.        String code = "";
  52.        if (input.contains("提取码")) {
  53.            code = input.substring(input.length() - 4);
  54.       }
  55.        // 提前http地址
  56.        Pattern pattern = Pattern.compile(
  57.                "\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" +
  58.                        "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" +
  59.                        "|mil|biz|info|mobi|name|aero|jobs|museum" +
  60.                        "|travel|[a-z]{2}))(:[\\d]{1,5})?" +
  61.                        "(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" +
  62.                        "((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
  63.                        "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" +
  64.                        "(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
  65.                        "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" +
  66.                        "(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
  67.        Matcher matcher = pattern.matcher(input);
  68.        if (matcher.find()) {
  69.            NetDiskUrl netDiskUrl = new NetDiskUrl();
  70.            netDiskUrl.setTitle(title);
  71.            netDiskUrl.setSubTitle(subTitle);
  72.            netDiskUrl.setUrl(matcher.group());
  73.            netDiskUrl.setCode(code);
  74.            return netDiskUrl;
  75.       }
  76.        return null;
  77.   }
  78.    public static void main(String[] args) {
  79.        try {
  80.            FileUtils.readText();
  81.       } catch (Exception e) {
  82.            e.printStackTrace();
  83.       }
  84.   }
  85. }

NetDiskUrl.java

  1. package com.bridge;
  2. public class NetDiskUrl {
  3.    private String title;
  4.    private String  subTitle;
  5.    private String url;
  6.    private String code;
  7.    @Override
  8.    public String toString() {
  9.        return "NetDiskUrl{" +
  10.                "title='" + title + '\'' +
  11.                ", subTitle='" + subTitle + '\'' +
  12.                ", url='" + url + '\'' +
  13.                ", code='" + code + '\'' +
  14.                '}';
  15.   }
  16.    public String getTitle() {
  17.        return title;
  18.   }
  19.    public void setTitle(String title) {
  20.        this.title = title;
  21.   }
  22.    public String getSubTitle() {
  23.        return subTitle;
  24.   }
  25.    public void setSubTitle(String subTitle) {
  26.        this.subTitle = subTitle;
  27.   }
  28.    public String getUrl() {
  29.        return url;
  30.   }
  31.    public void setUrl(String url) {
  32.        this.url = url;
  33.   }
  34.    public String getCode() {
  35.        return code;
  36.   }
  37.    public void setCode(String code) {
  38.        this.code = code;
  39.   }
  40. }

JDBCUtils.java

  1. package com.bridge.jdbc;
  2. import com.bridge.NetDiskUrl;
  3. import java.io.InputStream;
  4. import java.sql.*;
  5. import java.util.List;
  6. import java.util.Properties;
  7. /**
  8. * 封装数据库连接和关闭
  9. */
  10. public class JDBCUtils {
  11.    public static Connection getConnection() throws Exception {
  12.        //读取配置文件基本信息
  13.        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
  14.        Properties pros = new Properties();
  15.        pros.load(is);
  16.        String user = pros.getProperty("user");
  17.        String password = pros.getProperty("password");
  18.        String url = pros.getProperty("url");
  19.        String driverClass = pros.getProperty("driverClass");
  20.        //加载驱动
  21.        Class.forName(driverClass);
  22.        //获取连接
  23.        return DriverManager.getConnection(url, user, password);
  24.   }
  25.    public static void closeResource(Connection conn, Statement ps) {
  26.        try {
  27.            if (ps != null)
  28.                ps.close();
  29.       } catch (SQLException e) {
  30.            e.printStackTrace();
  31.       }
  32.        try {
  33.            if (conn != null)
  34.                conn.close();
  35.       } catch (SQLException e) {
  36.            e.printStackTrace();
  37.       }
  38.   }
  39.    public static void closeResource(Connection conn, Statement ps, ResultSet rs) {
  40.        try {
  41.            if (ps != null)
  42.                ps.close();
  43.       } catch (SQLException e) {
  44.            e.printStackTrace();
  45.       }
  46.        try {
  47.            if (conn != null)
  48.                conn.close();
  49.       } catch (SQLException e) {
  50.            e.printStackTrace();
  51.       }
  52.        try {
  53.            if (rs != null)
  54.                rs.close();
  55.       } catch (SQLException e) {
  56.            e.printStackTrace();
  57.       }
  58.   }
  59.    public static void insert() throws Exception {
  60.        Connection conn = JDBCUtils.getConnection();
  61.        Statement st = conn.createStatement();
  62.        for (int i = 1; i <= 20000; i++) {
  63.            String sql = "insert into goods(name) values('name_" + i + "')";
  64.            st.execute(sql);
  65.       }
  66.   }
  67.    public static void testInsert1(){
  68.        Connection conn = null;
  69.        PreparedStatement ps = null;
  70.        try {
  71.            long start = System.currentTimeMillis();//计算花费时间
  72.            conn = JDBCUtils.getConnection();
  73.            String sql = "insert into goods(name) values(?)";
  74.            ps = conn.prepareStatement(sql);
  75.            for(int i = 1;i<=20000;i++){
  76.                ps.setObject(1,"name_"+i);
  77.                ps.execute();
  78.           }
  79.            long end = System.currentTimeMillis();
  80.            System.out.println(end-start+"s");
  81.       }catch (Exception e){
  82.            e.printStackTrace();
  83.       }finally {
  84.            JDBCUtils.closeResource(conn,ps);
  85.       }
  86.   }
  87.    /**
  88.     * 批量提交
  89.     */
  90.    public static void batchCommitInsert(List<NetDiskUrl> list) {
  91.        Connection conn = null;
  92.        PreparedStatement ps = null;
  93.        try {
  94.            long start = System.currentTimeMillis();//计算花费时间
  95.            conn = JDBCUtils.getConnection();
  96.            conn.setAutoCommit(false);//不允许自动提交数据
  97.            String sql = "INSERT INTO test.t_baidu_url (id,title, sub_title, url, code) VALUES(? , ?, ?, ?, ?);";
  98.            ps = conn.prepareStatement(sql);
  99.            for (int i= 0;i < list.size();i++) {
  100.                NetDiskUrl diskUrl = list.get(i);
  101.                ps.setObject(1, i);
  102.                ps.setObject(2, diskUrl.getTitle());
  103.                ps.setObject(3, diskUrl.getSubTitle());
  104.                ps.setObject(4, diskUrl.getUrl());
  105.                ps.setObject(5, diskUrl.getCode());
  106.                //1、“攒”sql
  107.                ps.addBatch();
  108.                if (i % 500 == 0) {
  109.                    //2、执行batch
  110.                    ps.executeBatch();
  111.                    //3、清空batch
  112.                    ps.clearBatch();
  113.               }
  114.           }
  115.            //提交数据
  116.            conn.commit();
  117.            long end = System.currentTimeMillis();
  118.            System.out.println(end - start + "ms");
  119.       } catch (Exception e) {
  120.            e.printStackTrace();
  121.       } finally {
  122.            JDBCUtils.closeResource(conn, ps);
  123.       }
  124.   }
  125. }

Python webdriver.Chrome()的使用

webdriver.Chrome()的使用

1.前提 Python与Chrome路径下均安装chromedriver.exe。

2.chromedriver.exe版本选择及下载 下载地址为:CNPM Binaries Mirror Chrome版本查看:浏览器右上角三个点->帮助->关于Google Chrome

在这里插入图片描述

chromedriver.exe版本需要与浏览器版本一致或者接近:

在这里插入图片描述

3.安装 下载后解压,将 chromedriver.exe复制到下面两个目录中:

Chrome目录:比如C:\Program Files (x86)\Google\Chrome\Application Python目录:比如D:\Softwares\Python39

4.添加环境变量 将上述Chrome路径添加进系统环境光变量,Python使用时应该加入环境变量了,这个就不用管了。

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

Python实现自动分享

安装python3.8.X

Python Release Python 3.8.10 | Python.org

  1. from selenium import webdriver
  2. from selenium.webdriver.common.keys import Keys
  3. from selenium.webdriver.common.by import By
  4. import time
  5. import pymysql
  6. # 创建链接
  7. # 赋值给 conn连接对象
  8. # Python3.8。 我的修改方法:将 find_element_by_属性("value") 改为 find_element("By.属性","value")
  9. # 保存到百度云盘的方法
  10. def save_bdy(conn):
  11.    try:
  12.        # 获取数据库游标
  13.        cursor = conn.cursor()
  14.        # 查询保存了资源分享链接的res_url,和密码:captcha,状态为0表示没有处理
  15.        cursor.execute("select id rID, url, code from t_baidu_url where status = '0' ")
  16.        # 将保存好后的资源对应的记录状态标记为1
  17.        update_sql = "update t_baidu_url set status = '1' where id = %d "
  18.        results = cursor.fetchall()
  19.        # 创建Chrome浏览器,显示界面的,为了后面刚开始需要网盘登录时使用
  20.        browser = webdriver.Chrome()
  21.        #打开链接 30s内登录
  22.        browser.get("https://pan.baidu.com/")
  23.        time.sleep(30)
  24.        for (rID, url, code) in results:
  25.            try:
  26.                # 打开资源分享链接
  27.                browser.get(url)
  28.                # 判断是否有密码
  29.                if code:
  30.                    # 获取资源密码输入框
  31.                    pwd = browser.find_element(By.ID, 'accessCode')
  32.                    # 在框中输入密码
  33.                    pwd.send_keys(code)
  34.                    # 延时4秒
  35.                    time.sleep(4)
  36.                    # 发送回车键进入到资源页面
  37.                    pwd.send_keys(Keys.ENTER)
  38.                time.sleep(10)
  39.                # 找到“保存到网盘”的这个按钮
  40.                button = browser.find_element(By.CSS_SELECTOR, 'a[title="保存到网盘"]')
  41.                # 单击,注意这时候断点到这里停一下,如果如要认证,你可以填写认证可扫码认证都可以,
  42.                # 认证好后,会出现选择文件夹这个对话框,
  43.                # 只需要认证一次,后续出直接弹出这个文件选择框,自动进行即可
  44.                # 注意一定到断点,进你进行认证时后续程序不要执行
  45.                button.click()
  46.                time.sleep(5)
  47.                # 在文件夹选择对话框中选择你想到保存到的文件夹,比如我的资源
  48.                fold = browser.find_element(By.CSS_SELECTOR, 'span[node-path="/编程书籍大全"]')
  49.                # 单击这个文件夹就会选中
  50.                fold.click()
  51.                time.sleep(1)
  52.                # 然后获取确定按钮并单击就保存成功了
  53.                ok = browser.find_element(By.CSS_SELECTOR, 'a[title="确定"]')
  54.                ok.click()
  55.                time.sleep(5)
  56.                cur = conn.cursor()
  57.                try:
  58.                    # 保存成功后,就修改数据库记录的状态
  59.                    cur.execute(update_sql % rID)
  60.                    conn.commit()
  61.                except Exception as e:
  62.                    conn.roleback()
  63.                    print(e)
  64.                finally:
  65.                    cur.close()
  66.            # 异常忽略
  67.            except Exception as e:
  68.                print(e)
  69.    except Exception as e:
  70.        print(e)
  71. if __name__ == '__main__':
  72.    conn = pymysql.connect(
  73.        host='127.0.0.1',  # 本地回环地址
  74.        port=3306,  # 默认端口
  75.        user='xxx',  # 用户名 如:root
  76.        密码英文='xxx',  # 密码 如:root
  77.        database='test',  # 连接数据库名称
  78.        charset='utf8'  # 编码 不能写utf-8
  79.   )
  80.    save_bdy(conn)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/807782
推荐阅读
相关标签
  

闽ICP备14008679号