A detailed description of the use of the template in Python (urlparse)

Source: Internet
Author: User
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 '

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.