Here is a small series to bring you a Python (urlparse) template based on the use of a summary. Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.
First, Introduction
Urlparse module The user resolves the URL to 6 components and returns it as a tuple, returning 6 parts: scheme (protocol), Netloc (network location), path, params (path segment parameter), query, fragment (fragment).
Ii. List of functions
1, Urlparse.urlparse ()(resolves URL to component, 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= ')
Other attributes, such as (Username,password,hostname,port), are also included in the returned element:
>>> 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 ()(the relative address is combined into a URL, there is no limit to the input, the beginning must be HTTP//, otherwise the front will not be combined)
>>> urlparse.urljoin ("https://i.cnblogs.com", "editposts.aspx") ' https://i.cnblogs.com/EditPosts.aspx '
3, Urlparse.urlsplit (): Returns a tuple of 5 elements for URLs that follow 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 (): combined with urlsplit format into a URL, the passed element must be 5, or the decomposed tuples will be directly re-combined
>>> 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 (): Use the format of Urlparse combined into a URL, you can directly combine the return 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 '