【发布时间】:2017-03-20 23:27:23
【问题描述】:
我需要 lxml 来做两件事: 1) 列出 xml 文件中使用的所有各种前缀; 2) 指定前缀后,让 lxml 返回给我所有元素名称它们的多个属性。
对于这个 lxml:
<pref:MiscDetails contentRef='01-01_2016' misc='wha'>1000</pref:MiscDetails>
<pref:TestingThis contentRef='03-02_2017' misc='t' qual='5'>50</pref:TestingThis>
<pref:AnotherExample contentRef='01-01_2015' misc='x'>100000</pref:AnotherExample>
<test:AFinalExample contentRef='' te='t'>test</test:AFinalExample>
代码应该首先告诉我这个文件中的前缀是“pref”和“test”,然后我希望代码列出与“pref”关联的元素名称及其属性,然后是“test”。
输出 1:
"Listing prefixes:"
"pref"
"test"
输出 2:
"Listing the prefix 'pref' element names and their attributes:"
"Element MiscDetails with attributes contentRef='01-01_2016' misc='wha'"
"Element TestingThis with attributes contentRef='03-02_2017' misc='t' qual='5'"
"Element AnotherExample with attributes contentRef='01-01_2015' misc='x'"
"Listing the prefix 'test' element names and their attributes:"
"Element AFinalExample with attributes contentRef='' te='t'"
谢谢!
【问题讨论】:
-
到目前为止你尝试了什么?
标签: python xml web-scraping lxml