示例代码编译不通过
发布于 7 年前 作者 junguo 9723 次浏览 来自 官方Issues

# -*- coding: utf-8 -*-# filename: receive.pyimport xml.etree.ElementTree as ETdef parse_xml(web_data):

if len(web_data) == 0:

return None

xmlData = ET.fromstring(web_data)

msg_type = xmlData.find(‘MsgType’).text

if msg_type == ‘text’:

return TextMsg(xmlData)

elif msg_type == ‘image’:

return ImageMsg(xmlData)class Msg(object):

def __init__(self, xmlData):

self.ToUserName = xmlData.find(‘ToUserName’).text

self.FromUserName = xmlData.find(‘FromUserName’).text

self.CreateTime = xmlData.find(‘CreateTime’).text

self.MsgType = xmlData.find(‘MsgType’).text

self.MsgId = xmlData.find(‘MsgId’).textclass TextMsg(Msg):

def __init__(self, xmlData):

Msg.__init__(self, xmlData)

self.Content = xmlData.find(‘Content’).text.encode(“utf-8”)class ImageMsg(Msg):

def __init__(self, xmlData):

Msg.__init__(self, xmlData)

self.PicUrl = xmlData.find(‘PicUrl’).text

self.MediaId = xmlData.find(‘MediaId’).text

1 回复

看不懂,下一位

回到顶部