Skip to content

Instantly share code, notes, and snippets.

@BlaayLock
Forked from Artem-Mamchych/lxml example
Created September 6, 2017 09:28
Show Gist options
  • Save BlaayLock/3a89a8b2485db49319be948a2c681c48 to your computer and use it in GitHub Desktop.
Save BlaayLock/3a89a8b2485db49319be948a2c681c48 to your computer and use it in GitHub Desktop.
Пример использования lxml
# coding: utf8
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<soft>
<os>
<item name="linux" dist="ubuntu">
This text about linux
</item>
<item name="mac os">
Apple company
</item>
<item name="windows" dist="XP" />
</os>
</soft>'''
from lxml import etree
tree = etree.XML(xml) # Парсинг строки
#tree = etree.parse('1.xml') # Парсинг файла
nodes = tree.xpath('/soft/os/item') # Открываем раздел
for node in nodes: # Перебираем элементы
print node.tag,node.keys(),node.values()
print 'name =',node.get('name') # Выводим параметр name
print 'text =',[node.text] # Выводим текст элемента
# Доступ к тексту напрямую, с указанием фильтра
print 'text1',tree.xpath('/soft/os/item[@name="linux"]/text()')
print 'text2',tree.xpath('/soft/os/item[2]/text()')
# Доступ к параметру напрямую
print 'dist',tree.xpath('/soft/os/item[@name="linux"]')[0].get('dist')
# Выборка по ключу
print 'dist by key',tree.xpath('//*[@name="windows"]')[0].get('dist')
print 'iterfind:'
for node in tree.iterfind('.//item'): # поиск элементов
print node.get('name')
# Рекурсивный перебор элементов
print 'recursiely:'
def getn(node):
print node.tag,node.keys()
for n in node:
getn(n)
getn(tree.getroottree().getroot())
item ['name', 'dist'] ['linux', 'ubuntu']
name = linux
text = ['\n This text about linux\n ']
item ['name'] ['mac os']
name = mac os
text = ['\n Apple company\n ']
item ['name', 'dist'] ['windows', 'XP']
name = windows
text = [None]
text1 ['\n This text about linux\n ']
text2 ['\n Apple company\n ']
dist ubuntu
dist by key XP
iterfind:
linux
mac os
windows
recursiely:
soft []
os []
item ['name', 'dist']
item ['name']
item ['name', 'dist']
d = tree.xpath('//*')[0]
print etree.tostring(d)
print etree.tounicode(d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment