Python使用urllib时url带中文参数的问题
urllib.request.urlopen(url).read().decode('utf-8')
url中带中文参数会出错:UnicodeEncodeError: 'ascii' codec can't encode characters in position 84-86: ordinal not in range(128)
需要对中文进行转码
url = GEOCODE_BASE_URL + urllib.parse.quote('北京市')
文档中对urlib.parse.quote的描述:The URL quoting functions focus on taking program data and making it safe for use as URL components by quoting special characters and appropriately encoding non-ASCII text.
作者:包子头斯基
出处:http://www.cnblogs.com/colleen/
欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

浙公网安备 33010602011771号