您好我試圖使用此鏈接決議 xml: https ://docs.python.org/3/library/xml.etree.elementtree.html
但是,當我嘗試關注它時,我遇到了這個問題
>>> import xml.etree.ElementTree as ET
>>> tree = ET.parse('sitemap.xml')
>>> root = tree.getroot()
>>> print(root['loc'])
element indices must be integers
我正在嘗試決議loc此 sitemap.xml 宣告中的值:
<root>
<url>
<loc>HTTPS://website.com/</loc>
<lastmod>2022-10-10</lastmod>
</url>
<url>
<loc>https://website.com/search/</loc>
<lastmod>2022-10-10</lastmod>
</url>
<url>
<loc>https://website.com/auth/user/</loc>
</root>
更新
我可以通過以下方式列印出單個loc值: print(root[0][0].text)
但是,我想遍歷所有這些loc欄位并將它們列印出來 - 我該怎么做?
uj5u.com熱心網友回復:
tree = ET.parse('sitemap.xml')
root = tree.getroot()
for url in root:
for child in url:
if child.tag == 'loc':
print(child.text)
請注意,只有當 xml 的格式與您提供的完全相同時才會起作用(例如,所有loc標簽都是標簽的直接子代url)。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/515442.html
下一篇:谷歌表格:從文本中決議日期
