When we write a crawler, we may need to generate a new URL based on the current URL in the crawler. For example, the following pseudo code:
The running effect is shown in the figure below: But sometimes, the page turning parameter is not necessarily a number. For example, on some websites, visit a URL: https://xxx.com/articlelist?category=technology&after=asdrtJKSAZFD When you access this URL, it returns a JSON string, and this JSON contains the following fields:
This situation is more common in information flow websites. It can only scroll down infinitely to view the next page, and cannot jump directly by page number. The parameter after the next page is returned every time a request is made. When you want to access the next page, use this parameter to replace the parameter after after= in the current URL. In this way, replacing the parameters in the URL is not a simple task. Because the URL may have 4 situations:
You can try to cover these 4 situations and generate the URL of the next page using regular expressions. In fact, we don't need to use regular expressions. Python's built-in urllib module already provides a solution to this problem. Let's take a look at a piece of code:
The running effect is shown in the figure below: As can be seen from the figure, in these four cases, we can successfully add the parameter after = 0000000 for the next page. There is no need to consider how regular expressions can adapt to all situations. Among them, urlparse and urlunparse are a pair of opposite functions. The former converts the URL into a ParseResult object, and the latter converts the ParseResult object back to a URL string. The .query property of the ParseResult object is a string, which is the content after the question mark in the URL, in the following format: parse_qs and urlencode are also a pair of opposite functions. The former converts the string output by .query into a dictionary, while the latter converts the field into a string in the form of .query: After using parse_qs to convert the query into a dictionary, you can modify the parameter value and then convert it back again. Since the .query property of the ParseResult object is a read-only property and cannot be overwritten, we need to call an internal method ._replace to replace the new .query field and generate a new ParseResult object. Finally, convert it back to a URL. The above is what we introduced today, how to use the functions that come with urllib to replace the fields in the URL. |
<<: GSMA: 5G networks will cover two-fifths of the world's population by 2025
>>: 5G in numbers: 5G trends revealed by statistics in the first half of 2021
In this process, many applications are constantly...
Have you ever thought about how long it has been ...
CloudCone's Christmas Sale has begun. The mer...
Market research firm IDC expects the global smart...
[[269676]] Scene 1 The development and rise and f...
[51CTO.com Quick Translation] It’s a new year aga...
The Internet of Things is already booming, and it...
background With the rapid development of the Inte...
It's the golden March and April again, and I&...
iWebFusion (or iWFHosting) is a long-established ...
We have explained the configuration of switches m...
The first model I got to implement as part of my ...
This article will introduce the basic concepts, a...
We have previously shared Lisahost's US dual ...
Now 4G network signals have been popularized all ...