赞
踩
目录
本文可以学习了解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实现自动分享
#### 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】
FileUtils.java
- package com.bridge.file;
-
- import com.bridge.NetDiskUrl;
- import com.bridge.jdbc.JDBCUtils;
- import org.springframework.util.StringUtils;
-
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
-
- public class FileUtils {
-
- private static String title = "";
- private static String subTitle = "";
-
- public static void readText() throws Exception {
- File file = new File("C:\\Users\\Administrator\\Desktop\\temp.txt");
- FileInputStream fis = new FileInputStream(file);
- //Construct BufferedReader from InputStreamReader
- BufferedReader br = new BufferedReader(new InputStreamReader(fis));
- String line = null;
- List<NetDiskUrl> list = new ArrayList<>();
- while ((line = br.readLine()) != null) {
- NetDiskUrl netDiskUrl = extractUrls(line);
- if (netDiskUrl != null) {
- list.add(netDiskUrl);
- // System.out.println(netDiskUrl.toString());
- }
- }
- br.close();
- JDBCUtils.batchCommitInsert(list);
- }
-
- public static NetDiskUrl extractUrls(String input) {
- if (!StringUtils.hasText(input)) {
- return null;
- }
- input = input.trim();
- if (input.startsWith("####") && !input.startsWith("#####")) {
- if (input.contains("、")) {
- String[] split = input.split("、");
- title = split[1];
- subTitle = "";
- }
- }
- if (input.startsWith("#####")) {
- if (input.contains("、")) {
- String[] split = input.split("、");
- subTitle = split[1];
- }
- }
-
- String code = "";
- if (input.contains("提取码")) {
- code = input.substring(input.length() - 4);
- }
-
- // 提前http地址
- Pattern pattern = Pattern.compile(
- "\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" +
- "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" +
- "|mil|biz|info|mobi|name|aero|jobs|museum" +
- "|travel|[a-z]{2}))(:[\\d]{1,5})?" +
- "(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" +
- "((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
- "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" +
- "(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
- "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" +
- "(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
- Matcher matcher = pattern.matcher(input);
- if (matcher.find()) {
- NetDiskUrl netDiskUrl = new NetDiskUrl();
- netDiskUrl.setTitle(title);
- netDiskUrl.setSubTitle(subTitle);
- netDiskUrl.setUrl(matcher.group());
- netDiskUrl.setCode(code);
- return netDiskUrl;
- }
- return null;
- }
-
- public static void main(String[] args) {
- try {
- FileUtils.readText();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
-

NetDiskUrl.java
- package com.bridge;
-
- public class NetDiskUrl {
-
- private String title;
-
- private String subTitle;
-
- private String url;
-
- private String code;
-
- @Override
- public String toString() {
- return "NetDiskUrl{" +
- "title='" + title + '\'' +
- ", subTitle='" + subTitle + '\'' +
- ", url='" + url + '\'' +
- ", code='" + code + '\'' +
- '}';
- }
-
- public String getTitle() {
- return title;
- }
-
- public void setTitle(String title) {
- this.title = title;
- }
-
- public String getSubTitle() {
- return subTitle;
- }
-
- public void setSubTitle(String subTitle) {
- this.subTitle = subTitle;
- }
-
- public String getUrl() {
- return url;
- }
-
- public void setUrl(String url) {
- this.url = url;
- }
-
- public String getCode() {
- return code;
- }
-
- public void setCode(String code) {
- this.code = code;
- }
- }

JDBCUtils.java
- package com.bridge.jdbc;
-
- import com.bridge.NetDiskUrl;
-
- import java.io.InputStream;
- import java.sql.*;
- import java.util.List;
- import java.util.Properties;
-
- /**
- * 封装数据库连接和关闭
- */
-
- public class JDBCUtils {
- public static Connection getConnection() throws Exception {
- //读取配置文件基本信息
- InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");
- Properties pros = new Properties();
- pros.load(is);
- String user = pros.getProperty("user");
- String password = pros.getProperty("password");
- String url = pros.getProperty("url");
- String driverClass = pros.getProperty("driverClass");
- //加载驱动
- Class.forName(driverClass);
- //获取连接
- return DriverManager.getConnection(url, user, password);
- }
-
- public static void closeResource(Connection conn, Statement ps) {
- try {
- if (ps != null)
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- if (conn != null)
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
-
- public static void closeResource(Connection conn, Statement ps, ResultSet rs) {
- try {
- if (ps != null)
- ps.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- if (conn != null)
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- try {
- if (rs != null)
- rs.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
-
- public static void insert() throws Exception {
- Connection conn = JDBCUtils.getConnection();
- Statement st = conn.createStatement();
- for (int i = 1; i <= 20000; i++) {
- String sql = "insert into goods(name) values('name_" + i + "')";
- st.execute(sql);
- }
- }
-
- public static void testInsert1(){
- Connection conn = null;
- PreparedStatement ps = null;
- try {
- long start = System.currentTimeMillis();//计算花费时间
- conn = JDBCUtils.getConnection();
- String sql = "insert into goods(name) values(?)";
- ps = conn.prepareStatement(sql);
- for(int i = 1;i<=20000;i++){
- ps.setObject(1,"name_"+i);
- ps.execute();
- }
- long end = System.currentTimeMillis();
- System.out.println(end-start+"s");
- }catch (Exception e){
- e.printStackTrace();
- }finally {
- JDBCUtils.closeResource(conn,ps);
- }
- }
-
-
- /**
- * 批量提交
- */
- public static void batchCommitInsert(List<NetDiskUrl> list) {
- Connection conn = null;
- PreparedStatement ps = null;
- try {
- long start = System.currentTimeMillis();//计算花费时间
- conn = JDBCUtils.getConnection();
- conn.setAutoCommit(false);//不允许自动提交数据
- String sql = "INSERT INTO test.t_baidu_url (id,title, sub_title, url, code) VALUES(? , ?, ?, ?, ?);";
- ps = conn.prepareStatement(sql);
- for (int i= 0;i < list.size();i++) {
- NetDiskUrl diskUrl = list.get(i);
- ps.setObject(1, i);
- ps.setObject(2, diskUrl.getTitle());
- ps.setObject(3, diskUrl.getSubTitle());
- ps.setObject(4, diskUrl.getUrl());
- ps.setObject(5, diskUrl.getCode());
- //1、“攒”sql
- ps.addBatch();
- if (i % 500 == 0) {
- //2、执行batch
- ps.executeBatch();
- //3、清空batch
- ps.clearBatch();
- }
- }
- //提交数据
- conn.commit();
- long end = System.currentTimeMillis();
- System.out.println(end - start + "ms");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- JDBCUtils.closeResource(conn, ps);
- }
- }
- }

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
安装python3.8.X
Python Release Python 3.8.10 | Python.org
- from selenium import webdriver
- from selenium.webdriver.common.keys import Keys
- from selenium.webdriver.common.by import By
- import time
- import pymysql
-
- # 创建链接
- # 赋值给 conn连接对象
- # Python3.8。 我的修改方法:将 find_element_by_属性("value") 改为 find_element("By.属性","value")
- # 保存到百度云盘的方法
- def save_bdy(conn):
- try:
- # 获取数据库游标
- cursor = conn.cursor()
- # 查询保存了资源分享链接的res_url,和密码:captcha,状态为0表示没有处理
- cursor.execute("select id rID, url, code from t_baidu_url where status = '0' ")
- # 将保存好后的资源对应的记录状态标记为1
- update_sql = "update t_baidu_url set status = '1' where id = %d "
- results = cursor.fetchall()
- # 创建Chrome浏览器,显示界面的,为了后面刚开始需要网盘登录时使用
- browser = webdriver.Chrome()
- #打开链接 30s内登录
- browser.get("https://pan.baidu.com/")
- time.sleep(30)
- for (rID, url, code) in results:
- try:
- # 打开资源分享链接
- browser.get(url)
- # 判断是否有密码
- if code:
- # 获取资源密码输入框
- pwd = browser.find_element(By.ID, 'accessCode')
- # 在框中输入密码
- pwd.send_keys(code)
- # 延时4秒
- time.sleep(4)
- # 发送回车键进入到资源页面
- pwd.send_keys(Keys.ENTER)
- time.sleep(10)
- # 找到“保存到网盘”的这个按钮
- button = browser.find_element(By.CSS_SELECTOR, 'a[title="保存到网盘"]')
- # 单击,注意这时候断点到这里停一下,如果如要认证,你可以填写认证可扫码认证都可以,
- # 认证好后,会出现选择文件夹这个对话框,
- # 只需要认证一次,后续出直接弹出这个文件选择框,自动进行即可
- # 注意一定到断点,进你进行认证时后续程序不要执行
- button.click()
- time.sleep(5)
- # 在文件夹选择对话框中选择你想到保存到的文件夹,比如我的资源
- fold = browser.find_element(By.CSS_SELECTOR, 'span[node-path="/编程书籍大全"]')
- # 单击这个文件夹就会选中
- fold.click()
- time.sleep(1)
- # 然后获取确定按钮并单击就保存成功了
- ok = browser.find_element(By.CSS_SELECTOR, 'a[title="确定"]')
- ok.click()
- time.sleep(5)
- cur = conn.cursor()
- try:
- # 保存成功后,就修改数据库记录的状态
- cur.execute(update_sql % rID)
- conn.commit()
- except Exception as e:
- conn.roleback()
- print(e)
- finally:
- cur.close()
- # 异常忽略
- except Exception as e:
- print(e)
- except Exception as e:
- print(e)
-
- if __name__ == '__main__':
- conn = pymysql.connect(
- host='127.0.0.1', # 本地回环地址
- port=3306, # 默认端口
- user='xxx', # 用户名 如:root
- 密码英文='xxx', # 密码 如:root
- database='test', # 连接数据库名称
- charset='utf8' # 编码 不能写utf-8
- )
- save_bdy(conn)

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。