The method of python url splicing

  •  

  • Python's url is a common file link. A file contains multiple urls. In many websites, we need to splice multiple urls. On the Internet, we can often see the introduction of Python splicing methods, but many of them are very incomplete. Today we will take a look at a relatively complete python url splicing method. In Python, there are three ways we need to splice a url: The first one: directly splice multiple urls together (pip install python). The second: use the <data.co unt> tag in html. The third way: use the <path> tag in css. The first two methods are relatively simple, but if there are complex strings, it will be difficult to splice. For example in the above example we want to concatenate the two urls together. Using the third method, two urls can be spliced ​​together. However, there are several issues to be aware of when using this method:

    • 1. Two urls cannot have the same label

      We know that in python, two urls are distinguished by tags. For example: The code above uses tags to distinguish two urls. We know that tags in html only contain element names, for example: In the above example, we can see: (1) The first url uses the <data.co unt> tag to distinguish, and the second url uses <path> tag to distinguish. (2) In the first url we added a <title> element, but in the second url no element was added. We can see that a <title> element has been added to the second url. So there cannot be the same label between the two urls. If we want to splice two urls together, we only need to replace the <data. count> tag in html with <path>. The following code is to splice two urls together and sort the content at the same time.

    • 2. Among the two urls, one url must be undefined

      For example: In the above example, in the url we want to splice, the name in it is undefined, so the attribute "name" must be added. This attribute must be a string, because strings cannot be concatenated. For example: In the above example, the name attribute cannot be spliced. However, if a url contains multiple parameters, the second method can be used for splicing, but the url spliced ​​by this method is very irregular and confusing. Then just add the "list" attribute, because list is a string. The above two methods can realize the splicing of url, but the effect is different.

      • 1. Splicing url by judging the type of parameters

        Here we should pay attention that "[]" cannot be used when judging the parameter type, because the default Python parsing method uses Python built-in functions. For example: In the above example, we can see that the parameter "ab" is undefined, but we use a "[]" to concatenate. So the result of the splicing is chaotic. By judging the parameter type, we can determine whether the parameter is a string. If it is a string, then the following two situations will occur: When the parameter is a string, the following situations will occur: In this way to splice url, The splicing results are relatively standardized. However, this method is relatively slow and requires frequent use of concat statements for splicing.

      • 2. Concatenate urls by using the "list" attribute

        This attribute will have two values ​​in the url, the first is name, and the second is "list". Then we use name for splicing, and no error will occur, but if we use "list" for splicing, it will cause very confusing url. So how can we make URLs more standardized? In Python, we can use the `r'` function to achieve string concatenation. However, the url spliced ​​out using `r'` function has no rules. Then we need to make them orderly by setting some properties. Here we use the attribute of string type, that is, "list" to splice the url. After using `r'` function to splice urls, you can see that these urls have become very standardized. These urls are arranged in order, and each has a unique encoding.

    • 3. You must ensure that the lengths of the two urls are the same

      The label is a dynamic text label, and we can control whether it is automatically updated by writing a python code. For example, when we want to splice two urls, we can write: Then use pip install python () to splice multiple urls together. When we want to splice multiple urls, we only need to splice several strings together. For example, if we want to splice 20, 30, 40, 50, and 60, we only need to splice the previous strings together, and then splice the following strings together. For some complex situations, such as the string is empty, or the string is too long to be spliced ​​directly using the <data.count> tag, we can use the <path> tag for splicing. For example, in the above example, if we want to splice the string of 60, we can use the <path> tag for splicing. This method is somewhat similar to the above two methods, except that the length cannot be customized. The usage of this tag is very simple: when we want to match every character in the url, we can use this tag to match. In the above two examples, the <path> tag in html is used for matching. The above is my introduction to the python url splicing method. You can refer to it!

    • Fourth, the header tag should be separated from the content

      When using the header tag to stitch two URLs together, we can use some special parameters to set the header tag associated with this URL, for example: If we want to associate the header tag with the content, then we can do this: The following code sets a new url with different parameters. It should be noted that when using the <path> tag in css, the url needs to be processed. If there are many strings in the url, then this tag cannot be used. For example, in the above example, if we want to remove the "wo" string contained in the second url, we must remove the "wo" string contained in the second url.

The following are several commonly used Python URL splicing code examples:


1. Use the urllib.parse.urljoin() method to join URLs:
```python
from urllib.parse import urljoin
base_url = 'https://www.example.com/'
path = 'path/to/resource'
url = urljoin (base_url, path)
print(url) # Output https://www.example.com/path/to/resource
```
2. Use string splicing to splice the URL:
```python
base_url = 'https:// www.example.com/'
path = 'path/to/resource'
url = base_url + path
print(url) # output https://www.example.com/path/to/resource
```
3. use f- String splicing URL:
```python
base_url = 'https://www.example.com/'
path = 'path/to/resource'
url = f'{base_url}{path}'
print(url) # Output https://www.example.com/path/to/resource
```
4. Use urllib.parse.urlparse() and urllib.parse.urlunparse() methods to splice URLs:
```python
from urllib.parse import urlparse, urlunparse
base_url = 'https://www.example.com/'
path = 'path/to/resource'
parsed_url = urlparse(base_url)
parsed_url = parsed_url._replace(path=path)
url = urlunparse (parsed_url)
print(url) # output https://www.example.com/path/to/resource
```

Guess you like

Origin blog.csdn.net/qq_42751978/article/details/130636058