Detailed python urlencode encoding and URL stitching method

Source: Internet
Author: User

UrlEncode Calling Method

The UrlEncode parameter must be dictionary

Import urllibd = {' name1 ': ' www.pythontab.com ', ' name2 ': ' bbs.pythontab.com '}print urllib.urlencode (d)

Output:

Name2=bbs.pythontab.com&name1=www.pythontab.com

The equivalent of stitching two URL parameters, this usage is similar to PHP http_build_query (), here is not the majority of PHP in how to use, interested in their own to check.

UrlEncode encoding

The function UrlEncode does not alter the original encoding of the incoming parameter, which means that the encoding of the post or get parameter needs to be adjusted before the call.

Problem: Now the simulation request Google and Baidu, because Baidu is using gb2312 encoding, Google is using UTF8 encoding, two sites submitted to the URL of the Chinese parameter UrlEncode value is not the same, the following "Pythontab Chinese network" as an example :

# coding:utf-8str = U ' pythontab chinese net ' str = str.encode (' gb2312 ') d = {' name ': Str}q = Urllib.urlencode (d) Print Q

Results:

Name=pythontab%d6%d0%ce%c4%cd%f8

Note: The UrlEncode parameter must be dictionary

Other usage

Django is similar to UrlEncode in the following ways:

From django.utils.http Import Urlquotea = Urlquote (' pythontab Chinese net ') print a

Get the GBK code of Chinese characters

Urllib Converting strings

In fact, you can use the Urllib quote function to convert the Chinese in the URL, the Chinese to GBK encoding, the resulting encoding is the URL that conforms to the URI standard.

>>> import Urllib>>> a = "pythontab Chinese net" >>> a ' pythontab\xe4\xb8\xad\xe6\x96\x87\xe7\xbd\ X91 ' >>> urllib.quote (a) ' pythontab%e4%b8%ad%e6%96%87%e7%bd%91 ' >>>

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.