繁体   English   中英

使用来自 python 中的 lxml 请求的命名空间解析 xml

[英]parsing xml with namespace from request with lxml in python

我正在尝试从在线 xml 文件的表格中获取一些文本。 我可以找到表格:

from lxml import etree
import requests

main_file = requests.get('https://training.gov.au/TrainingComponentFiles/CUA/CUAWRT601_R1.xml')
main_file.encoding = 'utf-8-sig'
root = etree.fromstring(main_file.content)
tables = root.xpath('//foo:table', namespaces={"foo": "http://www.authorit.com/xml/authorit"})

print(tables)

但我不能再进一步了。 我正在寻找的文本是:

  1. 准备写脚本
  2. 编写草稿脚本
  3. 制作最终剧本

当我在此处粘贴 xml 时: http://xpather.com/

我可以使用以下表达式获取它: //table[1]/tr/td[@width="2700"]/p[@id="4"][not(*)]/text()

但这在这里行不通,我没有主意。 我怎样才能得到那个文本?

使用您声明的命名空间前缀(使用namespaces={"foo": "http://www.authorit.com/xml/authorit"} )例如代替//table[1]/tr/td[@width="2700"]/p[@id="4"][not(*)]/text()使用//foo:table[1]/foo:tr/foo:td[@width="2700"]/foo:p[@id="4"][not(*)]/text()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM