Created
January 18, 2012 22:36
-
-
Save Artem-Mamchych/1636265 to your computer and use it in GitHub Desktop.
Пример использования lxml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
d = tree.xpath('//*')[0] | |
print etree.tostring(d) | |
print etree.tounicode(d) |
А как положить в тег href переменную?
Допустим, если я хочу не вот так
linkElt = etree.SubElement(headElt, 'link', rel='stylesheet', href='mystyle.css', type='text/css')
а вот так
linkElt = etree.SubElement(headElt, 'link', rel='stylesheet', href=style_var, type='text/css')
\
Переменная строковая, но оно почему-то не принимает ее. Ссылка не отображается.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Дополню примером для генерации XML с помощью lxml
Вывод
Источник: http://infohost.nmt.edu/~shipman/soft/pylxml/web/creating.html