蟒蛇(新手)从API调用XML解析 [英] Python (newbie) Parse XML from API call

查看:88
本文介绍了蟒蛇(新手)从API调用XML解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找堆栈/文档一些教程/其他问题,但还是无法弄明白。唉!

发出API请求和解析出(要分配给变量,但是这是一个奖金这个问题),这就是我想要。为什么我不能列出的项目的标题和链接?

 #!的/ usr / bin中/蟒蛇对于替补#刮板屏
进口的urllib
从xml.etree进口的ElementTree作为ET秀=英雄
季=4
语言='恩'
极限='1'requestURL ='http://api.allsubs.org/index.php? \\
           +'搜索='+显示\\
           +'+ +的季节'+季\\
           +'&放大器;语言='+语言\\
           +'&放大器;限='+极限根= ET.parse(了urllib.urlopen(requestURL))。getroot()
打印根
打印的'\\ n'项目= root.findall(项目)
在项目的项目:
     。item.find(标题)文本#应该打印:其中,[CDATA [英雄第4季字幕]>!
     。item.find(链接)文本#应该打印:http://www.allsubs.org/subs-download/heroes+season+4/1223435/

XML响应

 < AllSubsAPI>
        <标题> AllSubs API:字幕搜索< /标题>
        &LT;链路GT; HTTP://www.allsubs.org< /链接&GT;
        &LT;描述&GT;&LT;![CDATA [字幕搜索英雄第4季]&GT;&LT; /描述&GT;
        &LT;语言&GT; EN-US&LT; /语言&GT;
        &lt;结果&GT; 1 LT; /结果&gt;
        &所述; found_results&GT; 24&下; / found_results&GT;
&LT;项目&GT;
    &LT;项目&GT;
            &LT;标题&GT;&LT;![CDATA [英雄第4季字幕]&GT;&LT; /标题&GT;
            &LT;链路GT; HTTP://www.allsubs.org/subs-download/heroes+season+4/1223435/< /链接&GT;
            &LT;&名GT; heroes-season-4-english-heroes-season-4-en.zip&LT; /文件名&GT;
            &LT; files_in_archive&GT;英雄 - 4x01-02 - Orientation.HDTV.FQM.en.srt |英雄 - 4x17 - Deception.HDTV.2HD.en.srt的艺术|英雄 - 4x07 - 斯特兰奇Attractors.HDTV.LOL.en。 SRT |英雄 - 4x08 - 黄飞鸿Texas.HDTV.2HD.en.srt一个时间|英雄 - 4x07 - 斯特兰奇Attractors.720p HDTV.DIMENSION.en.srt |英雄 - 4x05 - 癔症Blindness.720p HDTV.X264.en .SRT |英雄 - 4x09 - Shadowboxing.HDTV.LOL.en.srt |英雄 - 4X16 - 通Fail.HDTV.LOL.en.srt |英雄 - 4x04 - Acceptance.HDTV.en.srt |英雄 - 4x01-02 - Orientation.720p HDTV.DIMENSION.en.srt |英雄 - 4x06 - 塔布拉Rasa.HDTV.NoTV.en.srt |英雄 - 4×10 - 兄弟的Keeper.HDTV.FQM.en.srt |英雄 - 4x04 - Acceptance.HDTV.FQM .en.srt |英雄 - 4x14 - 让它Bleed.720p HDTV.DIMENSION.en.srt |英雄 - 4x06 - 塔布拉Rasa.720p HDTV.SiTV.en.srt |英雄 - 4x08 - 黄飞鸿Texas.HDTV一个时间.NoTV.en.srt |英雄 - 4×12 - 第五Stage.HDTV.LOL.en.srt |英雄 - 4x19 - 勇敢的新World.HDTV.LOL.en.srt |英雄 - 4x15 - 靠近You.720p HDTV。 DIMENSION.en.srt |英雄 - 4x03 - Ink.720p HDTV.DIMENSION.en.srt |英雄 - 4x11 - Thanksgiving.720p HDTV.DIMENSION.en.srt |英雄 - 4X13 - 在此Rock.720p HDTV.DIMENSION.en .SRT |英雄 - 4X13 - 在此Rock.HDTV.LOL.en.srt |英雄 - 4x14 - 让它Bleed.HDTV.LOL.en.srt |英雄 - 4x15 - 靠近You.HDTV.LOL.en.srt |英雄 - 4×12 - 第五Stage.720p HDTV.DIMENSION.en.srt |英雄 - 4x18 - 该Wall.HDTV.LOL.en.srt |英雄 - 4x08 - 黄飞鸿Texas.720p HDTV.CTU.en一个时间.SRT |英雄 - 4x17 - Deception.HDTV.CTU.en.srt的艺术|英雄 - 4x09 - Shadowboxing.720p HDTV.DIMENSION.en.srt |英雄 - 4×10 - 兄弟的Keeper.720p HDTV.DIMENSION.en.srt |英雄 - 4x04 - Acceptance.720p HDTV.CTU.en.srt |英雄 - 4x11 - Thanksgiving.HDTV.FQM.en.srt |英雄 - 4x03 - Ink.HDTV.FQM.en.srt |英雄 - 4x05 - 癔症性失明.HDTV.XII.en.srt |&LT; / files_in_archive&GT;
            &LT;语言&GT;连接&LT; /语言&GT;
            &LT;&ADDED_ON GT;&2010-02-16 LT; / ADDED_ON&GT;
    &LT; /项目&GT;&LT; /项目&GT;
&LT; / AllSubsAPI&GT;

更新:

这工作,感谢您的帮助,并指出我的错字

 项目= root.findall('项目/项目)
在项目的项目:
     打印item.find(标题)文本
     打印item.find(链接)。文本


解决方案

 项目= root.findall(项目)

 项目= root.findall('项目/项目)

I've look for some tutorials/other questions on stack/documentation and still can't figure it out. ugh!!!

Making the API request and the parsing out (want to assign to variables but that's a bonus to this question), This is what I'm trying. Why can't I list the title and link for the items?

#!/usr/bin/python

# Screen Scraper for Subs
import urllib
from xml.etree import ElementTree as ET

show = 'heroes'
season = '4'
language = 'en'
limit = '1'

requestURL = 'http://api.allsubs.org/index.php?' \
           + 'search=' + show \
           + '+season+' + season \
           + '&language=' + language \
           + '&limit=' + limit

root = ET.parse(urllib.urlopen(requestURL)).getroot()
print root
print '\n'

items = root.findall('items')
for item in items: 
     item.find('title').text # should print: <![CDATA[Heroes Season 4 Subtitles]]>
     item.find('link').text # Should print: http://www.allsubs.org/subs-download/heroes+season+4/1223435/

XML Response

        <AllSubsAPI> 
        <title>AllSubs API: Subtitles Search</title> 
        <link>http://www.allsubs.org</link> 
        <description><![CDATA[Subtitles Search for Heroes Season 4]]></description> 
        <language>en-us</language> 
        <results>1</results> 
        <found_results>24</found_results> 
<items> 
    <item> 
            <title><![CDATA[Heroes Season 4 Subtitles]]></title> 
            <link>http://www.allsubs.org/subs-download/heroes+season+4/1223435/</link> 
            <filename>heroes-season-4-english-heroes-season-4-en.zip</filename> 
            <files_in_archive>Heroes - 4x01-02 - Orientation.HDTV.FQM.en.srt|Heroes - 4x17 - The Art of Deception.HDTV.2HD.en.srt|Heroes - 4x07 - Strange Attractors.HDTV.LOL.en.srt|Heroes - 4x08 - Once Upon a Time in Texas.HDTV.2HD.en.srt|Heroes - 4x07 - Strange Attractors.720p HDTV.DIMENSION.en.srt|Heroes - 4x05 - Hysterical Blindness.720p HDTV.X264.en.srt|Heroes - 4x09 - Shadowboxing.HDTV.LOL.en.srt|Heroes - 4x16 - Pass Fail.HDTV.LOL.en.srt|Heroes - 4x04 - Acceptance.HDTV.en.srt|Heroes - 4x01-02 - Orientation.720p HDTV.DIMENSION.en.srt|Heroes - 4x06 - Tabula Rasa.HDTV.NoTV.en.srt|Heroes - 4x10 - Brother's Keeper.HDTV.FQM.en.srt|Heroes - 4x04 - Acceptance.HDTV.FQM.en.srt|Heroes - 4x14 - Let It Bleed.720p HDTV.DIMENSION.en.srt|Heroes - 4x06 - Tabula Rasa.720p HDTV.SiTV.en.srt|Heroes - 4x08 - Once Upon a Time in Texas.HDTV.NoTV.en.srt|Heroes - 4x12 - The Fifth Stage.HDTV.LOL.en.srt|Heroes - 4x19 - Brave New World.HDTV.LOL.en.srt|Heroes - 4x15 - Close to You.720p HDTV.DIMENSION.en.srt|Heroes - 4x03 - Ink.720p HDTV.DIMENSION.en.srt|Heroes - 4x11 - Thanksgiving.720p HDTV.DIMENSION.en.srt|Heroes - 4x13 - Upon This Rock.720p HDTV.DIMENSION.en.srt|Heroes - 4x13 - Upon This Rock.HDTV.LOL.en.srt|Heroes - 4x14 - Let It Bleed.HDTV.LOL.en.srt|Heroes - 4x15 - Close to You.HDTV.LOL.en.srt|Heroes - 4x12 - The Fifth Stage.720p HDTV.DIMENSION.en.srt|Heroes - 4x18 - The Wall.HDTV.LOL.en.srt|Heroes - 4x08 - Once Upon a Time in Texas.720p HDTV.CTU.en.srt|Heroes - 4x17 - The Art of Deception.HDTV.CTU.en.srt|Heroes - 4x09 - Shadowboxing.720p HDTV.DIMENSION.en.srt|Heroes - 4x10 - Brother's Keeper.720p HDTV.DIMENSION.en.srt|Heroes - 4x04 - Acceptance.720p HDTV.CTU.en.srt|Heroes - 4x11 - Thanksgiving.HDTV.FQM.en.srt|Heroes - 4x03 - Ink.HDTV.FQM.en.srt|Heroes - 4x05 - Hysterical Blindness.HDTV.XII.en.srt|</files_in_archive> 
            <languages>en</languages> 
            <added_on>2010-02-16</added_on> 
    </item> 

</items> 
</AllSubsAPI>

UPDATE:

This worked, thanks for the help and pointing out my typo

items = root.findall('items/item')
for item in items: 
     print item.find('title').text
     print item.find('link').text

解决方案

items = root.findall('items')

should be

items = root.findall('items/item')

这篇关于蟒蛇(新手)从API调用XML解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆