慕的地6264312
2022-12-20 15:24:49
我有一個看起來像這樣的 xml 文件(讓我們調(diào)用它是 abc.xml)。<?xml version="1.0" encoding="UTF-8"?><properties> <product name="XYZ" version="123"/> <application-links> <application-links> <id>111111111111111</id> <name>Link_1</name> <primary>true</primary> <type>applinks.ABC</type> <display-url>http://ABC.displayURL</display-url> <rpc-url>http://ABC.displayURL</rpc-url> </application-links> </application-links></properties>我的 python 代碼是這樣的f = open ('file.xml', 'r')from bs4 import BeautifulSoupsoup = BeautifulSoup(f,'lxml')print(soup.product)for applinks in soup.application-links: print(applinks)打印以下內(nèi)容<product name="XYZ" version="123"></product>Traceback (most recent call last): File "parse.py", line 7, in <module> for applinks in soup.application-links:NameError: name 'links' is not defined請你能幫我理解如何打印包含破折號/連字符'-'的標(biāo)簽的行
1 回答

白板的微信
TA貢獻(xiàn)1883條經(jīng)驗(yàn) 獲得超3個贊
我不知道beautifulsoup這里是否是最佳選擇,但我真的建議ElementTree像這樣在 python 中使用該模塊:
>>> import xml.etree.ElementTree as ET
>>> root = ET.parse('file.xml').getroot()
>>> for app in root.findall('*/application-links/'):
... print(app.text)
111111111111111
Link_1
true
applinks.ABC
http://ABC.displayURL
http://ABC.displayURL
因此,要打印<name>標(biāo)簽內(nèi)的值,您可以這樣做:
>>> for app in root.findall('*/application-links/name'):
... print(app.text)
Link_1
添加回答
舉報
0/150
提交
取消