def filter(self, text):
wanted_dm = ['created_at', 'id', 'text', 'source', 'sender_id', 'recipient_id', 'sender_screen_name', 'recipient_screen_name']
root = ET.fromstring(text)
builder = ET.TreeBuilder()
builder.start('direct-messages', {'type': 'array'})
for status in root.findall('direct_message'):
builder.start('direct_message', {})
for tag in wanted_status:
copy_element(builder, status, tag)
sender = status.find('sender')
builder.start('sender', {})
copy_element(builder, sender, 'profile_image_url')
builder.end('sender')
recipient = status.find('recipient')
builder.start('recipient', {})
copy_element(builder, recipient, 'profile_image_url')
builder.end('recipient')
builder.end('direct_message')
builder.end('direct-messages')
return ET.tostring(builder.close())
评论列表
文章目录