Pythonurlencode encoding and url splicing

Source: Internet
Author: User
Urlencode: The urlencode parameter must be Dictionaryimporturllibd {& amp; 39; name1 & amp; amp; 39;: & amp; 39; wwwpythontabcom & amp; 39;, & amp; 39; name2 &

Urlencode call 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

It is equivalent to splicing two url parameters. this usage is similar to http_build_query () in PHP. here, we won't know how to use it in PHP. if you are interested, check it yourself.

Urlencode encoding

The urlencode function does not change the original encoding of input parameters. that is to say, you need to adjust the encoding of post or get parameters before calling.

Problem: Now we simulate the request for Google and baidu. because baidu uses gb2312 encoding and google uses utf8 encoding, the urlencode values of the Chinese parameters submitted to the URL by the two sites are different, the following uses "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

Result:

name=PythonTab%D6%D0%CE%C4%CD%F8

Note: The urlencode parameter must be a Dictionary

Other usage

Urlencode in django is similar to the following method:

From django. utils. http import urlquotea = urlquote ('pythontab Chinese net') print

Obtain the GBK encoding of Chinese characters

Urllib conversion string

In fact, you can use the quote function of urllib to convert Chinese in the URL into GBK encoding. The resulting encoding is a URL that complies with the URI standard.

>>> Import urllib >>> a = "PythonTab 文"" >>> a' PythonTab \ xe4 \ xb8 \ xad \ xe6 \ x96 \ x87 \ xe7 \ xbd \ x91 '>> urllib. quote (a) 'pythontab % E4 % B8 % AD % E6 % 96% 87% E7 % BD % 91 '>
Related Article

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.