Home >Backend Development >Python Tutorial >python通过urllib2获取带有中文参数url内容的方法

python通过urllib2获取带有中文参数url内容的方法

WBOY
WBOYOriginal
2016-06-10 15:17:281302browse

本文实例讲述了python通过urllib2获取带有中文参数url内容的方法。分享给大家供大家参考。具体如下:

对于中文的参数如果不进行编码的话,python的urllib2直接处理会报错,我们可以先将中文转换成utf-8编码,然后使用urllib2.quote方法对参数进行url编码后传递。

content = u'你好 jb51.net'
content = content.encode('utf-8')
content = urllib2.quote(content)
api_url = 'http://www.jb51.net/q=%s'%content
res = urllib2.urlopen(api_url)

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn