あなたは直接の子のid
のをしたい場合は、次のXPathクエリを使用することができます。ここでは
# v obtain id attribute
document.xpath('//*[@id="stuff"]/*[@id]/@id')
# ^#stuff tag ^child with id attribute
<* id="stuff">
タグのための我々ので初見、そして私たちはどんな(直接の子を探してくださいタグ)は@id
で、@id
を取ります。
これはlxml.etree._ElementUnicodeResult
要素のリストを返します。ここで私たちが子供の種類に気を注意してください
[str(the_id) for the_id in document.xpath('//*[@id="stuff"]/*[@id]/@id')]
注:私たちは、しかし、文字列値を取得するためにstr(..)
を使用することができます。あなただけ<div>
子供のid
秒をしたい場合は、あなたが使用することができます。
# v obtain id attribute
document.xpath('//*[@id="stuff"]/div[@id]/@id')
# ^#stuff tag ^child with id attribute
をする場合には、あなたがのためにすべての子孫を見て、あなたは単に@id="stuff"
クエリと子供たちの間で、追加のスラッシュを追加する必要があります。
# v obtain id attribute
document.xpath('//*[@id="stuff"]//*[@id]/@id')
# ^#stuff tag ^descendant with id attribute
どのxpathを試しましたか? –
正直なところ、これにアプローチする方法が不明です。 div.xsを取得するために 'root.xpath( '// * [@ id =" stuff "]/div /')'を試しました – Abe