URL地址編碼和解碼

來源:互聯網
上載者:User

標籤:空格   ref   https   values   art   pytho   request   seq   should   

0. 參考【整理】關於http(GET或POST)請求中的url地址的編碼(encode)和解碼(decode)python3中的urlopen對於中文url是如何處理的?中文URL的編碼問題1. rfc1738
2.1. The main parts of URLs   A full BNF description of the URL syntax is given in Section 5.   In general, URLs are written as follows:       <scheme>:<scheme-specific-part>   A URL contains the name of the scheme being used (<scheme>) followed   by a colon and then a string (the <scheme-specific-part>) whose   interpretation depends on the scheme.   Scheme names consist of a sequence of characters. The lower case   letters "a"--"z", digits, and the characters plus ("+"), period   ("."), and hyphen ("-") are allowed. For resiliency, programs   interpreting URLs should treat upper case letters as equivalent to   lower case in scheme names (e.g., allow "HTTP" as well as "http").

注意字母不區分大小寫
2. python22.1
 1 >>> import urllib 2 >>> url = ‘http://web page.com‘ 3 >>> url_en = urllib.quote(url)    #空格編碼為“%20” 4 >>> url_plus = urllib.quote_plus(url)    #空格編碼為“+” 5 >>> url_en_twice = urllib.quote(url_en) 6 >>> url 7 ‘http://web page.com‘ 8 >>> url_en 9 ‘http%3A//web%20page.com‘10 >>> url_plus11 ‘http%3A%2F%2Fweb+page.com‘12 >>> url_en_twice13 ‘http%253A//web%2520page.com‘    #出現%25說明是二次編碼14 #相應解碼15 >>> urllib.unquote(url_en)16 ‘http://web page.com‘17 >>> urllib.unquote_plus(url_plus)18 ‘http://web page.com‘
2.2 URL含有中文
1 >>> import urllib2 >>> url_zh = u‘http://movie.douban.com/tag/美國‘3 >>> url_zh_en = urllib.quote(url_zh.encode(‘utf-8‘))    #參數為string4 >>> url_zh_en5 ‘http%3A//movie.douban.com/tag/%E7%BE%8E%E5%9B%BD‘6 >>> print urllib.unquote(url_zh_en).decode(‘utf-8‘)7 http://movie.douban.com/tag/美國
3. python33.1
 1 >>> import urllib 2 >>> url = ‘http://web page.com‘ 3 >>> url_en = urllib.parse.quote(url)    #注意是urllib.parse.quote 4 >>> url_plus = urllib.parse.quote_plus(url) 5 >>> url_en 6 ‘http%3A//web%20page.com‘ 7 >>> url_plus 8 ‘http%3A%2F%2Fweb+page.com‘ 9 >>> urllib.parse.unquote(url_en)10 ‘http://web page.com‘11 >>> urllib.parse.unquote_plus(url_plus)12 ‘http://web page.com‘
3.2 URl含中文
1 >>> import urllib2 >>> url_zh = ‘http://movie.douban.com/tag/美國‘3 >>> url_zh_en = urllib.parse.quote(url_zh)4 >>> url_zh_en5 ‘http%3A//movie.douban.com/tag/%E7%BE%8E%E5%9B%BD‘6 >>> urllib.parse.unquote(url_zh_en)7 ‘http://movie.douban.com/tag/美國‘
4. 其他
 1 >>> help(urllib.urlencode) 2 Help on function urlencode in module urllib: 3  4 urlencode(query, doseq=0) 5     Encode a sequence of two-element tuples or dictionary into a URL query string. 6  7     If any values in the query arg are sequences and doseq is true, each 8     sequence element is converted to a separate parameter. 9 10     If the query arg is a sequence of two-element tuples, the order of the11     parameters in the output will match the order of parameters in the12     input.13 14 >>>

 

URL地址編碼和解碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.