Urllib2.urlopen python chinese in windows
Question: I'm making a little scrip in python. The objective is to make a request to a
page that returns a Json file and work with the information. The problem is
that I need to work with chinese words in the urls. When I make the request
with (for example):

    
    
    f = urllib2.urlopen("http://maps.googleapis.com/maps/api/geocode/json?address=江苏省苏州市中国新加坡工业园区独墅湖科教创新区仁爱路111号&sensor=false")
    

In ubuntu I don't have problems and all is great, and give me the crrect json
file. But when I try in Windows the resquest fails (said me that the url
didn't exist). There is a problem with chinese caracters in windows with
urllib2?

The version of windows is 7, ubuntu 12.4. And I'm using python 2.7.

Thanks!

Marcos


Answer: You should urlencode the query:

    
    
    # -*- coding: utf-8 -*-
    from urllib import urlencode
    from urllib2 import urlopen
    
    params = dict(address=u"我不知道中国人。", sensor="false")
    query = urlencode([(k, v.encode('utf-8') if isinstance(v, unicode) else v)
                       for k, v in params.items()])
    r = urlopen("http://maps.googleapis.com/maps/api/geocode/json?" + query)
    # ...
    

