Urllib using four--urlencode

Source: Internet
Author: User
Tags urlencode

Urllib.urlencode

Convert dictionary data to URL encoding

 #  -*-coding:cp936-*- import   urllib  params  = { " score   ' : 100, '  name   ' :  "  Crawler basics   ", "  comment   ": "   Very good   " }qs  = Urllib.urlencode (params)  print  (QS) 

After encoding, follow the parameters passed behind the URL:

Comment=very+good&score=100&name=%c5%c0%b3%e6%bb%f9%b4%a1

Reverse

In the Urlparse bag.

Urlparse.parse_qs Method--Return dictionary

 import   Urllib  import   Urlparseparams  = { "  Score   ": 100,"  name   ' :  "  Crawler basics   ", "  comment   ": "   Very good   " }qs  = Urllib.urlencode (params) dic  = Urlparse.parse_qs (QS)  print  (DIC) 

Operation Result:

{'comment': ['very good'score ' : [' ['] 'name': ['\xc5\xc0\xb3\ Xe6\xbb\xf9\xb4\xa1']}

To extract parameters for a URL:

# -*-coding:cp936-*- Import Urllib Import  'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd =python%20%e5%8f%91%e9%80%81%e9%82%ae%e4%bb%b6&rsv_pq=8ddee8730003c015&rsv_t=fc02p4%2by% 2fpkzafftaqgcglcxpvpvojkm6zg7pgczzb%2fezaqkxhsuwrpphqs&rqlang=cn&rsv_enter=1&rsv_sug3=14&rsv_ sug1=10&rsv_sug7=101'= urlparse.urlparse (URL)print(Result)

Get result first using the Urlparse.urlparse (URL) method

Parseresult (scheme='https', netloc='www.baidu.com', path= ' / S ', params=', query='Ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu &wd=python%20%e5%8f%91%e9%80%81%e9%82%ae%e4%bb%b6&rsv_pq=8ddee8730003c015&rsv_t=fc02p4%2by% 2fpkzafftaqgcglcxpvpvojkm6zg7pgczzb%2fezaqkxhsuwrpphqs&rqlang=cn&rsv_enter=1&rsv_sug3=14&rsv_ sug1=10&rsv_sug7=101', fragment=')

Extract from query parameter in result

Import Urllib Import  'https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=0&rsv_idx=1&tn=baidu&wd =python%20%e5%8f%91%e9%80%81%e9%82%ae%e4%bb%b6&rsv_pq=8ddee8730003c015&rsv_t=fc02p4%2by% 2fpkzafftaqgcglcxpvpvojkm6zg7pgczzb%2fezaqkxhsuwrpphqs&rqlang=cn&rsv_enter=1&rsv_sug3=14&rsv_ sug1=10&rsv_sug7=101'==  Urlparse.parse_qs (result.query  )print(DIC)

Operation Result:

{'WD': ['python \xe5\x8f\x91\xe9\x80\x81\xe9\x82\xae\xe4\xbb\xb6'],'F': ['8'],'Rsv_enter': ['1'],'RSV_BP': ['0'],'rsv_t': ['Fc02p4+y/pkzafftaqgcglcxpvpvojkm6zg7pgczzb/ezaqkxhsuwrpphqs'],'Rsv_idx': ['1'],'TN': ['Baidu'],'Rqlang': ['cn'],'Rsv_sug7': ['101'],'RSV_PQ': ['8ddee8730003c015'],'RSV_SUG1': ['Ten'],'Rsv_sug3': [' -'],'IE': ['Utf-8']}

Urllib using four--urlencode

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.