【问题标题】:xml.etree.ElementTree returns Object adress instead of Valuexml.etree.ElementTree 返回对象地址而不是值
【发布时间】:2021-03-23 15:01:38
【问题描述】:

我正在尝试使用 xml.etree.ElementTree 从 xml 中获取价值

from urllib.request import urlopen
import xml.etree.ElementTree as ET

with urlopen('http://127.0.0.1:8088/api/?') as f:
    tree = ET.parse(f)
    root = tree.getroot()

print(root.findall('recording'))

但我得到的是:

<Element 'recording' at 0x000001D871642E50>

第 4 行搜索到的 url-request 内容为 False:

<vmix>
  <version>24.0.0.51</version>
  <edition>Trial</edition>
  <recording>False</recording>
</vmix>

附: print(root.findall('recording').text) 也不起作用:

AttributeError: 'list' object has no attribute 'text'attribute text is not 

【问题讨论】:

    标签: python xml python-requests xml-parsing elementtree


    【解决方案1】:

    findall() 返回一个列表,因此您需要在列表中迭代以访问文本属性

    您也可以使用 find() 返回找到的第一个“录音”元素

    for records in root.findall('recording'):
       print(r.text)
    
    //or
    
    print(root.find('recording').text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 2022-10-16
      相关资源
      最近更新 更多