簡體   English   中英

Python 3.4-XML解析-IndexError:列表索引超出范圍-如何找到XML范圍?

[英]Python 3.4 - XML Parse - IndexError: List Index Out of Range - How do I find range of XML?

好的,伙計們,我是解析XML和Python的新手,並且正在嘗試使其工作。 如果有人可以幫助我,將不勝感激。 如果您可以幫助我(教育我)如何自己解決問題,那就更好了!

我無法找出XML文檔的引用范圍,因為我找不到任何文檔。 這是我的代碼,之后將包括整個Traceback。

#import library to do http requests:
import urllib.request

#import easy to use xml parser called minidom:
from xml.dom.minidom import parseString
#all these imports are standard on most modern python implementations

#download the file:
file = urllib.request.urlopen('http://www.wizards.com/dndinsider/compendium/CompendiumSearch.asmx/KeywordSearch?Keywords=healing%20%word&nameOnly=True&tab=')
#convert to string:
data = file.read()
#close file because we dont need it anymore:
file.close()
#parse the xml you downloaded
dom = parseString(data)
#retrieve the first xml tag (<tag>data</tag>) that the parser finds with name tagName:
xmlTag = dom.getElementsByTagName('Data.Results.Power.ID')[0].toxml()
#strip off the tag (<tag>data</tag>  --->   data):
xmlData=xmlTag.replace('<id>','').replace('</id>','')
#print out the xml tag and data in this format: <tag>data</tag>
print(xmlTag)
#just print the data
print(xmlData)

追溯

/usr/bin/python3.4 /home/mint/PycharmProjects/DnD_Project/Power_Name.py
Traceback (most recent call last):
  File "/home/mint/PycharmProjects/DnD_Project/Power_Name.py", line 14, in <module>
xmlTag = dom.getElementsByTagName('id')[0].toxml()
IndexError: list index out of range

流程以退出代碼1完成

print len( dom.getElementsByTagName('id') )

編輯:

ids = dom.getElementsByTagName('id')

if len( ids ) > 0 :
     xmlTag = ids[0].toxml()
     # rest of code

編輯:我添加示例,因為我在其他評論中看到了,你不知道如何使用它

順便說一句:我在代碼中添加了一些有關文件/連接的注釋

import urllib.request

from xml.dom.minidom import parseString

# create connection to data/file on server
connection = urllib.request.urlopen('http://www.wizards.com/dndinsider/compendium/CompendiumSearch.asmx/KeywordSearch?Keywords=healing%20%word&nameOnly=True&tab=')

# read from server as string (not "convert" to string):
data = connection.read()

#close connection because we dont need it anymore:
connection.close()

dom = parseString(data)

# get tags from dom
ids = dom.getElementsByTagName('Data.Results.Power.ID')

# check if there are any data
if len( ids ) > 0 :
    xmlTag = ids[0].toxml()
    xmlData=xmlTag.replace('<id>','').replace('</id>','')
    print(xmlTag)
    print(xmlData)
else:
    print("Sorry, there was no data")

或者如果有更多標簽,可以使用for循環

dom = parseString(data)

# get tags from dom
ids = dom.getElementsByTagName('Data.Results.Power.ID')

# get all tags - one by one
for one_tag in ids:
    xmlTag = one_tag.toxml()
    xmlData = xmlTag.replace('<id>','').replace('</id>','')
    print(xmlTag)
    print(xmlData)

BTW:

  • getElementsByTagName()需要標記名ID而不是路徑Data.Results.Power.ID
  • 標記名是ID因此您必須替換<ID>而不是<id>
  • 對於此標簽,您可以事件使用one_tag.firstChild.nodeValue代替xmlTag.replace

dom = parseString(data)

# get tags from dom
ids = dom.getElementsByTagName('ID') # tagname

# get all tags - one by one
for one_tag in ids:
    xmlTag = one_tag.toxml()
    #xmlData = xmlTag.replace('<ID>','').replace('</ID>','')
    xmlData = one_tag.firstChild.nodeValue
    print(xmlTag)
    print(xmlData)

我已經有一段時間沒有使用內置的xml庫了,但是Mark Pilgrim的Dive into Python一書中對此進行了介紹。

-我輸入的內容表明您的問題已經得到解答,但是由於您提到了Python的新知識,所以我認為您會發現該文本對於xml解析很有用,並且是該語言的絕佳介紹。

如果您想嘗試另一種解析xml和html的方法,我強烈建議使用lxml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM