Python (urlparse) template usage summary, pythonurlparse
I. Introduction
The urlparse module parses the url into six components and returns them as tuples. The returned six parts are: scheme (Protocol), netloc (network location), and path (path), params, query, and fragment ).
Ii. Function list
1. urlparse. urlparse () (parse the url as a component and the url must start with http)
>>> urlparse.urlparse("https://i.cnblogs.com/EditPosts.aspx?opt=1")ParseResult(scheme='https', netloc='i.cnblogs.com', path='/EditPosts.aspx', params='', query='opt=1', fragment='')
The returned element also contains other attributes, such as username, password, hostname, port ):
>>> urlparse.urlparse("https://i.cnblogs.com:80/EditPosts.aspx?opt=1").port80
>>> urlparse.urlparse("https://i.cnblogs.com:80/EditPosts.aspx?opt=1").hostname'i.cnblogs.com'
2. urlparse. urljoin () (combine the relative address into a url. There is no restriction on the input. It must start with http: //; otherwise, it will not be combined)
>>> urlparse.urljoin("https://i.cnblogs.com","EditPosts.aspx")'https://i.cnblogs.com/EditPosts.aspx'
3. urlparse. urlsplit (): returns a tuples of five elements, applicable to URLs following RFC2396
>>> urlparse.urlsplit("https://i.cnblogs.com:80/EditPosts.aspx?opt=1")SplitResult(scheme='https', netloc='i.cnblogs.com:80', path='/EditPosts.aspx', query='opt=1', fragment='')
4. urlparse. urlunsplit (): Use the urlsplit format to combine a url. Five elements must be transmitted, or the decomposed tuples must be recombined.
>>> urlparse.urlunsplit(("https","i.cnblogs.com","EditPosts.aspx","a=a","b=b"))'https://i.cnblogs.com/EditPosts.aspx?a=a#b=b'
>>> parse = urlparse.urlsplit("https://i.cnblogs.com:80/EditPosts.aspx?opt=1")>>> urlparse.urlunsplit(parse)'https://i.cnblogs.com:80/EditPosts.aspx?opt=1'
5. urlparse. urlunparse (): Combine the urlparse format into a url. You can directly combine the returned values of urlparse.
>>> parse = urlparse.urlparse("https://i.cnblogs.com:80/EditPosts.aspx?opt=1")>>> urlparse.urlunparse(parse)'https://i.cnblogs.com:80/EditPosts.aspx?opt=1'
>>> urlparse.urlunparse(("https","i.cnblogs.com","/EditPosts.aspx","","opt=1",""))'https://i.cnblogs.com/EditPosts.aspx?opt=1'