使用Python请求

时间:2017-06-18 08:34:21

标签: python encoding utf-8 python-requests

当我尝试使用requests发布数据时遇到了麻烦。 我需要发布由cp932进行URL编码的日文字符数据,但数据在requests中由utf-8重新编码。

所以我的问题是:有没有办法避免使用utf-8编码,或以任何方式指定编码?

如何重现

# -*- coding: utf-8 -*-
import sys
import urllib.parse
import requests

encoded_str = urllib.parse.quote_plus('あいう', encoding='cp932')
html = requests.post('http://example.com/', data={'data1':encoded_str})
sys.stdout.write(html.request.body + '\n')

预期结果

字符串"あいう"由cp932编码。

data1=%82%A0%82%A2%82%A4

我得到的结果

字符串"あいう"由cp932编码并由utf-8重新编码。

data1=%2582%25A0%2582%25A2%2582%25A4

系统信息

  • Mac OS 10.12.5
  • Python 3.6.1
  • 请求2.18.1

0 个答案:

没有答案