在一个python for循环中将2个不同的XML属性一起解析

时间:2014-04-17 16:55:44

标签: python xml python-2.7

我希望我的标题有点清晰,因为我不太清楚如何用它来表达它。

我正在编写一个脚本来从我的Plex服务器的XML输出中提取电影标题,并在以后将它们提供给其他功能。我已经能够毫无问题地提取title属性,并在for循环中正确使用它。我现在正在尝试解析collection属性,并在if/and循环中插入for循环以获得一些额外的逻辑。

XML片段我正在解析:

<Video ratingKey="1308" key="/library/metadata/1308" studio="Marvel Studios" type="movie" title="The Avengers" titleSort="Avengers" contentRating="16+" summary="When an unexpected enemy emerges and threatens global safety and security, Nick Fury, director of the international peacekeeping agency known as S.H.I.E.L.D., finds himself in need of a team to pull the world back from the brink of disaster. Spanning the globe, a daring recruitment effort begins!" rating="7" viewOffset="4433638" lastViewedAt="1397229827" year="2012" tagline="Some assembly required." thumb="/library/metadata/1308/thumb/1396546317" art="/library/metadata/1308/art/1396546317" duration="8574897" originallyAvailableAt="2012-05-04" addedAt="1393760099" updatedAt="1396546317">
<Media videoResolution="1080" id="1213" duration="8574897" bitrate="2200" width="1920" height="1080" aspectRatio="1.78" audioChannels="2" audioCodec="aac" videoCodec="h264" container="mp4" videoFrameRate="24p" optimizedForStreaming="1" has64bitOffsets="0">
<Part id="4105" key="/library/parts/4105/file.mp4" duration="8574897" file="M:\The Avengers (2012)\The Avengers (2012).mp4" size="2358359343" container="mp4" has64bitOffsets="0" optimizedForStreaming="1"/>
</Media>
<Genre tag="Action"/>
<Writer tag="Joss Whedon"/>
<Director tag="Joss Whedon"/>
<Country tag="USA"/>
<Role tag="Robert Downey Jr."/>
<Role tag="Chris Evans"/>
<Role tag="Mark Ruffalo"/>
<Collection tag="Save Me"/>
</Video>

我目前拥有的Python代码:

plex_url = 'http://localhost:32400/library/sections/2/all'
root_tree = minidom.parse(urllib.urlopen(plex_url))
video = root_tree.getElementsByTagName('Video')
for t in video:
    title = t.getAttribute('title') 
    global collection
    collection = root_tree.getAttribute('Collection')
    print collection
    movie_data(title)
    if Netflix() is True and collection == 'Save Me':
        print "%s is available on Netflix, but marked for saving. Moving to next movie." % movie
        continue
    elif collection != 'Save Me' and Netflix() is True:
        choice = raw_input("Netflix Streaming is available for this movie. Do you wish to mark this movie for deletion?")
        if choice in yes:
            delete_movie()
        elif choice in no:
            print "Skipping %s. Moving on to next movie." % movie
            continue
        else:
            sys.stdout.write("Please respond with 'yes' or 'no'")
    else:
        print "%s is not available on Netflix. Moving on to next movie." % movie
        continue

我遇到的问题是即使影片具有Collection属性,它似乎也没有捕获它,因此collection变量永远不会'Save Me'

我做错了什么?

1 个答案:

答案 0 :(得分:0)

当您想要一个具有当前&#34;视频&#34;上的标记属性的Collection元素时,您正在根节点上搜索Collection属性。元件。而不是:

global collection
collection = root_tree.getAttribute('Collection')

collections = t.getElementsByTagName('Collection')
if collections:
    collection = collections[0].getAttribute('tag')