我正在尝试检索XML文件中特定xml标记的值。问题在于它返回的是内存地址,而不是实际值。
已经尝试过使用其他库的多种方法。真正没有产生结果。
from xml.etree import ElementTree
tree = ElementTree.parse('C:\\Users\\Sid\\Desktop\\Test.xml')
root = tree.getroot()
items = root.find("items")
item= items.find("item")
print(item)
预计为1 2 3 4.实际:内存地址。 XML文件是:
<data>
<items>
<item>1</item>
</items>
<items>
<item>2</item>
</items>
<items>
<item>3</item>
</items>
<items>
<item>4</item>
</items>
</data>
答案 0 :(得分:1)
from bs4 import BeautifulSoup
import urllib
test = '''<data>
<items>
<item>1</item>
</items>
<items>
<item>2</item>
</items>
<items>
<item>3</item>
</items>
<items>
<item>4</item>
</items>
</data>'''
soup = BeautifulSoup(test, 'html.parser')
data = soup.find_all("item")
for d in data:
print(d.text)
输出:
1
2
3
4
from xml.etree import ElementTree
tree = ElementTree.parse('list.txt')
root = tree.getroot()
items = root.findall("items")
for elem in items:
desired_tag = elem.find("item")
print(desired_tag.text)
输出:
1
2
3
4
编辑:
如果要以空格分隔的一行打印它们。
print(desired_tag.text, "\t", end = "")