我想将html文件解析为:
1) 当td中有子模式时,请输出stage1
2) 当td中没有子模式时,请输出stage2
如何完成我的代码?你知道吗
data='''
<table>
<tr>
<td>
<span> hallo
</span>
</td>
</tr>
<tr>
<td> hallo
</td>
</tr>
</table> '''
import lxml.html
root=lxml.html.document_fromstring(data)
set=root.xpath('//table//tr//td')
for cell in set:
if(there is a child node in current node):
print("stage1")
else:
print("stage2")
一种方法是使用
getchildren()
方法:印刷品:
因为第一个
td
有span
在里面,第二个td
没有任何子级。你知道吗升级版本:
相关问题 更多 >
编程相关推荐