推荐学习书目
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 · Apr 7, 2013 · 6392 views
    This topic created in 4828 days ago, the information mentioned may be changed or developed.
    代码里面这样实现的:
    co = etree.Element('Content')
    co.text = etree.CDATA(u"欢迎光临!")

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

    请教各位大拿,应该怎么破?
    顺便吐槽一下:公众平台的API,还真是难用!
    Supplement 1  ·  Apr 8, 2013
    &# 27426; &# 36814; &# 20809; &# 20020; &# 65281;
    用户收到的是上述这串字符(去掉空格)。

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

    在回复里面加上了html的head信息如下,也不顶用。
    <meta http-equiv="Content-Type" content="text/html; charset=utf8" />
    Supplement 2  ·  Apr 8, 2013
    感谢@cloverstd ,问题已经解决。

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

    xml.etree.ElementTree.tostring(element, encoding="us-ascii", method="xml")
    pengdu
        2
    pengdu  
    OP
       Apr 8, 2013
    @cloverstd 感谢大拿,搞定了!

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

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

    代码最开始加了 #-*- encoding:utf8 -*-,后面就没管编码,以为是默认utf8了。
    gamexg
        3
    gamexg  
       Apr 8, 2013
    &#60;&#47;&#100;&#105;&#118;&#62; 没有过滤?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3039 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 08:54 · PVG 16:54 · LAX 01:54 · JFK 04:54
    ♥ Do have faith in what you're doing.