pyqt实现截取资源链接中的百度网盘链接

一个有一点点作用的小工具。每次获取到资源的时候 一般格式为:
XXXX+链接+https链接 + 提取码:xxxx。
或者
XXXX+链接+https链接?pwd + 提取码:xxxx
例如:
1.链接:https://pan.baidu.com 提取码:33om
2.链接: https://pan.baidu.com?pwd=p73q 提取码: p73q

每次复制粘贴到浏览器的时候,感觉操作很繁琐,特别是第一种情况,还需要手动输出密码。
如此就想写一个工具,去除链接中其他的文件信息,提取出整个链接,并且将第一种也变为第二种的格式。
ui设计由pyqt5实现:

界面如上图所示。
有密码的链接意为:链接中带有?xxxx。

执行示例:
链接1:链接 https://pan.baidu.com/s/?pwd=tq3e 提取码: tq3e
链接2:链接https://pan.baidu.com/s/ 提取码: 3b6i


点击复制,也能将链接复制到剪切板上。

py文件的代码为:
其中使用到了正则表达式进行匹配。

import os

from PyQt5.QtWidgets import *
from PyQt5 import uic, QtWidgets
import sys
import re


def processPath(path):
    '''
    :param path: 相对于根目录的路径
    :return: 拼接好的路径
    '''
    if getattr(sys, 'frozen', False):  # 判断是否存在属性frozen,以此判断是打包的程序还是源代码。false为默认值,即没有frozen属性时返回false
        base_path = sys._MEIPASS  # 该属性也是打包程序才会有,源代码尝试获取该属性会报错
    else:
        base_path = os.path.abspath(".")  # 当源代码运行时使用该路径
    return os.path.join(base_path, path)


class MyWindows(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi(processPath('baidu_link.ui'))

        # 增加底层文字
        self.ui.re_link1.setPlaceholderText('这里输入有密码的链接')
        self.ui.re_link2.setPlaceholderText('这里输入无密码的链接')

        # 设置采集模块为只读模式
        self.ui.link_res1_2.setReadOnly(True)
        self.ui.link_res2.setReadOnly(True)

        # 信号绑定
        self.ui.bt1.clicked.connect(lambda: self.fun_one(self.ui.re_link1))
        self.ui.bt2.clicked.connect(lambda: self.fun_two(self.ui.re_link2))
        self.ui.copy1.clicked.connect(lambda:self.copy_to_clipboard_one())
        self.ui.copy2.clicked.connect(lambda:self.copy_to_clipboard_two())

        self.ui.show()

    def fun_one(self, link_one):
   
        link = link_one.text()
        url_pattern = r'https?://[^\s]+'
        urls = re.findall(url_pattern, link)
        res_link_one = urls[0]
        self.ui.link_res1_2.setText(res_link_one)

    def copy_to_clipboard_one(self):
        # 获取文本并复制到剪切板
        text = self.ui.link_res1_2.toPlainText()
        print(f"准备获取的文本是: '{text}'")  # 输出当前的文本
        print(text)
        if text:
            clipboard = QApplication.clipboard()
            clipboard.setText(text)
            QMessageBox.information(self, "成功", "文本已复制到剪切板!")
        else:
            QMessageBox.warning(self, "警告", "文本框为空!")

    def fun_two(self, link_two):
      
        link = link_two.text()
        url_pattern = r'https?://[^\s]+'
        urls = re.findall(url_pattern, link)
        pwd = link[-4:]
        print(pwd)
        res_link_two = urls[0] + "?" + pwd
        self.ui.link_res2.setText(res_link_two)

    def copy_to_clipboard_two(self):
        # 获取文本并复制到剪切板
        text = self.ui.link_res2.toPlainText()
        if text:
            clipboard = QApplication.clipboard()
            clipboard.setText(text)
            QMessageBox.information(self, "成功", "文本已复制到剪切板!")
        else:
            QMessageBox.warning(self, "警告", "文本框为空!")



if __name__ == '__main__':
    app = QApplication(sys.argv)
    myapp = MyWindows()
    sys.exit(app.exec_())

ui文件的代码为

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>link_res1</class>
 <widget class="QMainWindow" name="link_res1">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>820</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="0" column="0">
     <widget class="QGroupBox" name="groupBox">
      <property name="title">
       <string>有密码的链接截取</string>
      </property>
      <layout class="QHBoxLayout" name="horizontalLayout_2">
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout">
         <item>
          <widget class="QLabel" name="label">
           <property name="font">
            <font>
             <weight>50</weight>
             <italic>false</italic>
             <bold>false</bold>
            </font>
           </property>
           <property name="text">
            <string>输入原始链接:</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QLineEdit" name="re_link1"/>
         </item>
         <item>
          <widget class="QPushButton" name="bt1">
           <property name="font">
            <font>
             <weight>75</weight>
             <bold>true</bold>
            </font>
           </property>
           <property name="text">
            <string>点击截取</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QLabel" name="label_2">
           <property name="font">
            <font>
             <weight>50</weight>
             <italic>false</italic>
             <bold>false</bold>
            </font>
           </property>
           <property name="text">
            <string>截取后的链接:</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="copy1">
           <property name="text">
            <string>点击复制</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QTextEdit" name="link_res1_2"/>
         </item>
        </layout>
       </item>
      </layout>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="QGroupBox" name="groupBox_2">
      <property name="title">
       <string>无密码的链接截取</string>
      </property>
      <layout class="QHBoxLayout" name="horizontalLayout_3">
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout_4">
         <item>
          <widget class="QLabel" name="label_3">
           <property name="font">
            <font>
             <weight>50</weight>
             <italic>false</italic>
             <bold>false</bold>
            </font>
           </property>
           <property name="text">
            <string>输入原始链接:</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QLineEdit" name="re_link2"/>
         </item>
         <item>
          <widget class="QPushButton" name="bt2">
           <property name="font">
            <font>
             <weight>75</weight>
             <bold>true</bold>
            </font>
           </property>
           <property name="text">
            <string>点击截取</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QLabel" name="label_4">
           <property name="font">
            <font>
             <weight>50</weight>
             <italic>false</italic>
             <bold>false</bold>
            </font>
           </property>
           <property name="text">
            <string>截取后的链接:</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QPushButton" name="copy2">
           <property name="text">
            <string>点击复制</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QTextEdit" name="link_res2"/>
         </item>
        </layout>
       </item>
      </layout>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>820</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

另外,上一篇博客讲述了如何使用pyinstaller打包这两个文件,有兴趣的可以去试试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值