V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
chendajun
V2EX  ›  Python

Python3.x urllib 或 requests 可以 post 中文吗?

  •  
  •   chendajun · 2016-06-17 16:00:24 +08:00 · 4867 次点击
    这是一个创建于 2876 天前的主题,其中的信息可能已经有所发展或是发生改变。
    13 条回复    2016-06-19 14:16:59 +08:00
    WinterWu
        1
    WinterWu  
       2016-06-17 16:07:51 +08:00 via iPhone
    为啥会有这个问题?仅标题而言,当然可以。啥文字都行,只要服务器肯收。
    chendajun
        2
    chendajun  
    OP
       2016-06-17 16:17:58 +08:00
    @WinterWu hi ,我是 Python 小白。具体流程是这样的:我这边需 post 一个 json (里面包含中文)到系统里面,如果 encode 后服务器那边接收到的就不是中文了。
    aitaii
        3
    aitaii  
       2016-06-17 16:20:25 +08:00
    接收到后 decode ?
    imn1
        4
    imn1  
       2016-06-17 16:31:42 +08:00
    字节传输而已, post 图片都行
    sxmman
        5
    sxmman  
       2016-06-17 16:34:14 +08:00
    当然可以,中文其实也是数字编码。
    chendajun
        6
    chendajun  
    OP
       2016-06-17 16:36:07 +08:00
    @aitaii 接收数据接口涉及到其他项目,改接口动静太大。 so 只能我这边想办法解决
    chendajun
        7
    chendajun  
    OP
       2016-06-17 16:37:04 +08:00
    @sxmman 不会是接收到后 decode 吧?
    sxmman
        8
    sxmman  
       2016-06-17 16:43:06 +08:00
    @chendajun 浏览器 form post 的一般都是 utf8 的,接收到后不用 decode ,直接存入数据库,如果要解码处理的话, decode 到 unicode 也行。
    WinterWu
        9
    WinterWu  
       2016-06-17 18:04:14 +08:00
    @chendajun
    1. post 过去是什么就是什么,这个都是看后台如何写的。根本不需要做什么 encode 。
    2. 如果你的问题是 post 过去的中文乱码了?是不是服务器中文没有使用 utf8 编码,大家都用 utf8 就好了。或者你匹配后端的编码格式。
    3. 其实这个和 python 没关系,这就是标准 http 呀,你用 curl\postman 等工具都可以 post 。 python post 方式和他们一样的,只是用 python 代码实现而已。
    RTNelo
        10
    RTNelo  
       2016-06-17 19:28:30 +08:00
    JSON 中是使用\uXXXX 来表示 Unicode 字符的,如果 LZ 用的是 json.dumps 后 POST 的话, POST 的内容应该都是 ASCII 码,所以不存在是否能够 POST 中文的问题。
    所以 LZ 大概是服务端的编码设置有问题,或者并没有按照标准生成 JSON 字符串(自己拼接的)?
    Exin
        11
    Exin  
       2016-06-17 20:05:33 +08:00 via Android
    decode 为 Unicode 再 JSON 化然后 post
    chendajun
        12
    chendajun  
    OP
       2016-06-19 13:26:30 +08:00
    @RTNelo json 是按照服务器端规定的格式拼接的,之前用 Python2.x 的时候直 post 中文。
    RTNelo
        13
    RTNelo  
       2016-06-19 14:16:59 +08:00
    @chendajun 所以 POST 出去的并不是标准的 JSON 咯?这种情况下最好直接使用 json.dumps ,可以省很多事(前提是服务端在根据 JSON 标准解析你发送过去的字符串)。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2216 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 09:40 · PVG 17:40 · LAX 02:40 · JFK 05:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.