Python (urlparse) template usage summary, pythonurlparse

Source: Internet
Author: User

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'

 

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.