我试图从 xml 中获取价值,使用 xml.etree.ElementTree
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>
搜索到的 url-request 内容在第 4 行为 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
答案 0 :(得分:0)
findall() 返回一个列表,因此您需要在列表中迭代以访问文本属性
您还可以使用 find() 返回找到的第一个“记录”元素
for records in root.findall('recording'):
print(r.text)
//or
print(root.find('recording').text)