第一个situation:tag内有link
from lxml import etree
node = etree.fromstring('<a xml = 'www.www.com'><c>bum</c></a>')
print node.findtext('c',default = 'what happened?')
第二个situation:所求text所在tag上一级tag无content
from lxml import etree
node = etree.fromstring('<a><b><c>bum</c></b></a>')
print node.findtext('c', default = 'what happened?')
以上两个situation所返回的结果都是‘None’
成功的situation
from lxml import etree
node = etree.fromstring('<a><c>bum</c></a>')
print node.findtext('c')
想问一下这两个问题应该怎么解决?
就回答你第二個情況如下,第一個情況可以類比
print( node.findtext('.//c', default = 'what happened?'))
print( node.xpath('//c/text()')[0] )
Python3 執行結果為
bum
bum