A few lines of code, network disk link head to see!

Posted by sparq on Mon, 10 Jan 2022 19:57:12 +0100


In the evening, when my girlfriend came back from work, I invited her downstairs for a walk. She looked sad and said she would work overtime tonight.

After careful inquiry, I learned that: my girlfriend received a task from her boss today, and the leader threw her a file, which is full of Baidu online disk links and extraction codes. She needs to extract the files in the online disk before the end of today.

I was really a little distressed to hear her say that she would stay up late to work. So I asked her instinctively, which link affects the efficiency most in the work now, and I'll help her. She told me that it's hard to see the links in Baidu online disk. It's a technical job to copy links and extract codes. Because of other text interference, it's often impossible to copy.

The link is similar to the following:

Link: https://pan.baidu.com/s/1ctcXiZymWst2NC_JPDkr4Q Extraction code: j1ub copy this content and open Baidu online disk mobile App, which is more convenient to operate

You must be familiar with this link. Not only Baidu network disk, but also many network disks are like this.

In that case, of course I'll help her solve this difficult problem.


In fact, her demand is very simple. Extract the online disk link and corresponding extraction code from the shared text.

To solve this problem, we only need to use regular expression to match the URL and extraction code in the shared text.

Implementation analysis

The code is very simple, directly:

url_pattern = 'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
code_pattern = '(?<=Extraction code: )[0-9a-z]{4}'
url_regex = re.compile(url_pattern)
code_regex = re.compile(code_pattern)

Next, test:

str = 'link: https://pan.baidu.com/s/1ctcXiZymWst2NC_JPDkr4Q extraction code: j1ub copy this content and open Baidu online disk mobile App, which is more convenient to operate. '

You can see two lines of printout on the console. The first line is the link and the second line is the extraction code:

https://pan.baidu.com/s/1ctcXiZymWst2NC_JPDkr4Q j1ub

A few lines of code, it's done!

Wait, this is so programmer thinking!

Now it's just a program. Do you want her to copy some text into my program and run it again? Isn't this more troublesome???

Of course not. I still have some product literacy!

Of course, give her an interface. I searched my knowledge base and prepared to use tkinter to draw a simple interface for her to use.

The implementation is as follows:

def draw_window(self):
        self.init_window = Tk()  # Instantiate a parent window
        self.init_window.title("Baidu online disk link extraction tool_v1.0")  # Window name
        # Source information
        self.init_data_label = Label(self.init_window, text="Extracted information copied")
        self.init_data_label.grid(row=0, column=0)
        self.init_data_text = Text(self.init_window, width=100, height=5, borderwidth=1, relief="solid")  # Original data entry box
        self.init_data_text.grid(row=1, column=0, columnspan=10)

        # Button
        self.str_trans_button = Button(self.init_window, text="extract", width=10, height=2, bg="blue",
                                       command=self.extractData)  # Call internal method plus () as direct call
        self.str_trans_button.grid(row=2, column=2)

        # link
        self.link_data_label = Label(self.init_window, width=10, text="link")
        self.link_data_label.grid(row=3, column=0, columnspan=1)
        self.link_data_text = Text(self.init_window, width=60, height=2, borderwidth=1, relief="solid")
        self.link_data_text.grid(row=3, column=1, columnspan=6)

        # Extraction code
        self.code_data_label = Label(self.init_window, width=10, text="Extraction code")
        self.code_data_label.grid(row=3, column=7, columnspan=1)
        self.code_data_text = Text(self.init_window, width=20, height=2, borderwidth=1, relief="solid")
        self.code_data_text.grid(row=3, column=8, columnspan=2)

The above is the code for drawing an interface. After running, it will look like this:

Ugly is a little ugly, but time is tight and the task is heavy. Use it first.

After combining with the parsing code, the normal operation should be as follows:

Of course, it can also be further transformed. For example, after obtaining the online disk link and extraction code, directly use "selenium" to automatically control the browser to open the corresponding Baidu online disk page, and the girlfriend can directly select the file on the page and click download.

But there's not enough time tonight. Let her use it first.


To deal with a simple requirement, we use regular and drawing interfaces. We can also use selenium, which simulates the operation of the browser. It can be seen how important the accumulation of knowledge is at ordinary times. Little friends should consciously accumulate some practical technologies at ordinary times. They can come at hand when the demand comes, rather than "hate less when the book comes to use"!

Thank everyone who is willing to read my article. I am also a beginner and learning about new media creation. Creation is a very worthwhile thing to continue to invest in, because your support every time is a great affirmation to me!

In order to facilitate my friends who want to learn python, I set up an exchange group and uploaded it

Many python learning materials can be obtained for free by small partners in need.