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