In Python, if you want to extract information from an HTTP address (usually a URL), such as protocol (http or https), host name (domain name or IP address), path, etc., you can use the urlparse function in the module. Here is an example of how to use urlparse to intercept different parts of an HTTP address:
Import module
First, you need to import the module:
from import urlparse
Use the urlparse function
Then, you can useurlparse
Functions to parse URLs:
url = "/path/to/resource?key=value#fragment" parsed_url = urlparse(url)
Access various parts of the parsed URL
urlparse
Returns a named tuple of six elements (or a named tuple of 11 elements in new versions of Python, including query parameters and fragments), and these parts can be accessed through the attribute name:
print("Scheme:", parsed_url.scheme) # Protocol (e.g. http or https)print("Netloc:", parsed_url.netloc) # Network location (e.g.)print("Path:", parsed_url.path) # Path (for example /path/to/resource)print("Params:", parsed_url.params) # Parameters (uncommon, usually empty)print("Query:", parsed_url.query) # Query parameters (for example, key=value)print("Fragment:", parsed_url.fragment) # Fragment identifier(For example fragment)
The complete code is as follows:
from import urlparse url = "/path/to/resource?key=value#fragment" parsed_url = urlparse(url) print("Scheme:", parsed_url.scheme) # Output: httpprint("Netloc:", parsed_url.netloc) # Output:print("Path:", parsed_url.path) # Output: /path/to/resourceprint("Query:", parsed_url.query) # Output: key=valueprint("Fragment:", parsed_url.fragment) # Output: fragment
Python intercepts url splicing content
import as urlparse def urlIntercept(url): parse = (url) querys = urlparse.parse_qs() querys1 = {k: v[0] for k, v in ()} # print(querys1) return querys1 a = 'http://192.168.110:9090/test/api/v1?A=&B=1212&C=VZABCDEw0u74Xrm8cTtyhg==' ve = urlIntercept(a) print(ve) print(ve["A"])
This is the end of this article about how to implement Url address interception in Python. For more related content in Python Url address interception, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!