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
pengdu
V2EX  ›  Python

微信公众平台API,怎么回复中文?

  •  
  •   pengdu ·
    pengdu · 2013-04-07 23:11:21 +08:00 · 5493 次点击
    这是一个创建于 4044 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码里面这样实现的:
    co = etree.Element('Content')
    co.text = etree.CDATA(u"欢迎光临!")

    微信收到的消息:
    欢迎光临!

    请教各位大拿,应该怎么破?
    顺便吐槽一下:公众平台的API,还真是难用!
    第 1 条附言  ·  2013-04-08 07:03:02 +08:00
    &# 27426; &# 36814; &# 20809; &# 20020; &# 65281;
    用户收到的是上述这串字符(去掉空格)。

    这些是utf8编码在html中的显示,比如在v2ex就会像正文里一样,直接显示为“欢迎光临!”
    但是在微信里面看到,还是这些字符串,不知道怎么破。

    在回复里面加上了html的head信息如下,也不顶用。
    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
    第 2 条附言  ·  2013-04-08 09:42:21 +08:00
    感谢@cloverstd ,问题已经解决。

    lxml.etree在tostring()的时候,忘了指明encoding='utf-8'了。
    3 条回复    1970-01-01 08:00:00 +08:00
    cloverstd
        1
    cloverstd  
       2013-04-08 07:19:04 +08:00 via Android   ❤️ 2
    用 ET 的 tostring 要指定 UTF-8 编码

    xml.etree.ElementTree.tostring(element, encoding="us-ascii", method="xml")
    pengdu
        2
    pengdu  
    OP
       2013-04-08 07:58:33 +08:00
    @cloverstd 感谢大拿,搞定了!

    print lxml.etree.tostring(rsp, encoding="utf-8")
    这样就ok了

    之前写的:print lxml.etree.tostring(rsp)

    代码最开始加了 #-*- encoding:utf8 -*-,后面就没管编码,以为是默认utf8了。
    gamexg
        3
    gamexg  
       2013-04-08 09:37:08 +08:00
    &#60;&#47;&#100;&#105;&#118;&#62; 没有过滤?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2232 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 05:53 · PVG 13:53 · LAX 22:53 · JFK 01:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.