Это ElementTree Element
object.
Вы также можете посмотреть lxml API documentation, в котором есть lxml.etree._Element
page. Эта страница рассказывает вам о каждом отдельном атрибуте и методе в этом классе, о котором вы могли бы когда-либо узнать.
Я бы начал с чтения lxml.etree
tutorial, однако.
Если элемент не может быть проиндексирован, он является пустым тегом и нет дочерних узлов для извлечения.
Чтобы найти все строки по Bernardo
, требуется выражение XPath с картой пространства имен. Не имеет значения, какой префикс используется, если это не является пустой строкой lxml
будет отображать его на правильный URL пространства имен:
nsmap = {'s': 'http://www.tei-c.org/ns/1.0'}
for line in tree.xpath('.//s:sp[@who="Barnardo"]/s:l/text()', namespaces=nsmap):
print line.strip()
Это извлекает весь текст в <l>
элементах, которые содержатся в <sp who="Barnardo">
тегах , Обратите внимание на префиксы s:
на имена тегов, словарь nsmap
сообщает lxml
, какое пространство имен использовать. Я напечатал их без окружающих лишних пробелов.
Для вашего образца документа, который дает:
>>> for line in tree.xpath('.//s:sp[@who="Barnardo"]/s:l/text()', namespaces=nsmap):
... print line.strip()
...
Who's there?
Long live the king!
He.
'Tis now struck twelve; get thee to bed, Francisco.
Have you had quiet guard?
Well, good night.
If you do meet Horatio and Marcellus,
The rivals of my watch, bid them make haste.
Say,
What, is Horatio there?
Welcome, Horatio: welcome, good Marcellus.
I have seen nothing.
Sit down awhile;
And let us once again assail your ears,
That are so fortified against our story
What we have two nights seen.
Last night of all,
When yond same star that's westward from the pole
Had made his course to illume that part of heaven
Where now it burns, Marcellus and myself,
The bell then beating one,
In the same figure, like the king that's dead.
Looks 'a not like the king? mark it, Horatio.
It would be spoke to.
See, it stalks away!
How now, Horatio! you tremble and look pale:
Is not this something more than fantasy?
What think you on't?
I think it be no other but e'en so:
Well may it sort that this portentous figure
Comes armed through our watch; so like the king
That was and is the question of these wars.
'Tis here!
It was about to speak, when the cock crew.
попробовать: 'etree.tostring (XML)' –
Великий, который работает, но как я могу получить данные из определенного тега? – Jono
@Jono: было бы намного проще помочь вам, если бы вы показали содержимое «ham.xml» или, по крайней мере, образец его, если он очень большой. –