【发布时间】:2011-10-08 13:24:45
【问题描述】:
如果介于 66 之间,我正在尝试删除所有内容:
我收到以下错误:TypeError: 'NoneType' 类型的参数不可迭代...如果 element.tag == 'answer' and '-66' in element.text:
这有什么问题?有什么帮助吗?
#!/usr/local/bin/python2.7
# -*- coding: UTF-8 -*-
from lxml import etree
planhtmlclear_utf=u"""
<questionaire>
<question>
<questiontext>What's up?</questiontext>
<answer></answer>
</question>
<question>
<questiontext>Cool?</questiontext>
<answer>-66</answer>
</question>
</questionaire>
"""
html = etree.fromstring(planhtmlclear_utf)
questions = html.xpath('/questionaire/question')
for question in questions:
for element in question.getchildren():
if element.tag == 'answer' and '-66' in element.text:
html.xpath('/questionaire')[0].remove(question)
print etree.tostring(html)
【问题讨论】: