1. 程式人生 > >urllib url解析學習

urllib url解析學習

utf-8 args www. baidu 請求 dcl AS 必須 中文

#!/usr/bin/env python  
# encoding: utf-8  
from urllib.parse import *
#urlparse:解析url分段
#urlsplit:類似urlparse,不再單獨解析params部分
#urlunsplit:後面必須1個列表,裏面5個參數
#urljoin:字符串的拼接
#parse_qs:字符串參數轉字典參數
#parse_qsl:字符串參數轉列表元組
#urlencode:GET請求參數逇編碼
#quote:url帶中文的時候可用來編碼
#unquote:用來解碼
URL="http://www.baidu.com/s?wd=falsk%20%E6%95%B0%E6%8D%AE%E6%93%8D%E4%BD%9C&rsv_spt=1&rsv_iqid=0x869d926300006682&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=0&oq=kwargs&inputT=6623&rsv_t=121540vUUWCKfciS1Ir0wlIBRBImaOVodO0fliZnJ%2BDCLWcUQ%2BHb5lhrg204XikhTHMY&rsv_pq=ef5d22fb0001200c&sug=python%2520utf-8&rsv_sug3=125&rsv_sug1=36&rsv_sug7=100&rsv_sug2=0&rsv_sug4=7658" #scheme表示http還是https的協議,如果url不帶協議根據scheme的值加上 result1=urlparse(URL,scheme=‘‘,allow_fragments=True) print(urlunsplit([result1.scheme,result1.netloc,"","",""])) # ParseResult(scheme=‘http‘, netloc=‘www.baidu.com‘, path=‘/s‘, params=‘‘, query=‘wd=falsk%20%E6%95%B0%E6%8D%AE%E6%93%8D%E4%BD%9C&rsv_spt=1&rsv_iqid=0x869d926300006682&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=0&oq=kwargs&inputT=6623&rsv_t=121540vUUWCKfciS1Ir0wlIBRBImaOVodO0fliZnJ%2BDCLWcUQ%2BHb5lhrg204XikhTHMY&rsv_pq=ef5d22fb0001200c&sug=python%2520utf-8&rsv_sug3=125&rsv_sug1=36&rsv_sug7=100&rsv_sug2=0&rsv_sug4=7658‘, fragment=‘‘)
#scheme表示http還是https的協議,如果url不帶協議根據scheme的值加上
result1=urlparse(URL,scheme=‘‘,allow_fragments=True)


print(urlunsplit([result1.scheme,result1.netloc,"","",""]))
# ParseResult(scheme=‘http‘, netloc=‘www.baidu.com‘, path=‘/s‘, params=‘‘, query=‘wd=falsk%20%E6%95%B0%E6%8D%AE%E6%93%8D%E4%BD%9C&rsv_spt=1&rsv_iqid=0x869d926300006682&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=0&oq=kwargs&inputT=6623&rsv_t=121540vUUWCKfciS1Ir0wlIBRBImaOVodO0fliZnJ%2BDCLWcUQ%2BHb5lhrg204XikhTHMY&rsv_pq=ef5d22fb0001200c&sug=python%2520utf-8&rsv_sug3=125&rsv_sug1=36&rsv_sug7=100&rsv_sug2=0&rsv_sug4=7658‘, fragment=‘‘)

  

urllib url解析學習