python - 雅虎体育 API XML 命名空间找不到元素

标签 python xml namespaces elementtree default-namespace

因此,在使用雅虎的 Sports API 进行查询后,我开始尝试使用 python 中的 lxml 解析 XML:我尝试在将元素存储在根目录中后获取它们,并且如果我简单地使用,我可以访问子标签索引;然而调用像 root.find("season") 这样的东西不会返回任何结果。根据我读过的其他帖子,我相信这可能与 xml 文件中使用的命名空间有关。 我应该如何处理这个问题,以便我可以获得我需要的元素?

编辑:我很抱歉不够清楚。我正在尝试观察 NFL 四分卫安德鲁·拉克 (Andrew Luck)。该代码访问 root[0][1]。 root[0] 应该是玩家标签,但玩家标签内部有很多子标签,因此 root[0][0] 是它的第一个子标签,即player_key标签,而 root[0][1] 是玩家标签的第二个子标签,这应该是player_id标签。

但是调用 root[0].find('player_id') 或 find 的任何变体,iter 方法不会返回任何内容。

再次感谢您的帮助!

下面是xml代码

<?xml version="1.0" encoding="UTF-8"?>
<fantasy_content xml:lang="en-US" yahoo:uri="http://fantasysports.yahooapis.com/fantasy/v2/player/348.p.25711/stats" time="36.563873291016ms" copyright="Data provided by Yahoo! and STATS, LLC" refresh_rate="31" xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" xmlns="http://fantasysports.yahooapis.com/fantasy/v2/base.rng">
 <player>
  <player_key>348.p.25711</player_key>
  <player_id>25711</player_id>
  <name>
   <full>Andrew Luck</full>
   <first>Andrew</first>
   <last>Luck</last>
   <ascii_first>Andrew</ascii_first>
   <ascii_last>Luck</ascii_last>
  </name>
  <editorial_player_key>nfl.p.25711</editorial_player_key>
  <editorial_team_key>nfl.t.11</editorial_team_key>
  <editorial_team_full_name>Indianapolis Colts</editorial_team_full_name>
  <editorial_team_abbr>Ind</editorial_team_abbr>
  <bye_weeks>
   <week>10</week>
  </bye_weeks>
  <uniform_number>12</uniform_number>
  <display_position>QB</display_position>
  <headshot>
   <url>http://l.yimg.com/iu/api/res/1.2/sm2sm277cd6oNXrZU.uiSQ--/YXBwaWQ9c2hhcmVkO2NoPTIzMzY7Y3I9MTtjdz0xNzkwO2R4PTg1NztkeT0wO2ZpPXVsY3JvcDtoPTYwO3E9MTAwO3c9NDY-/https://s.yimg.com/xe/i/us/sp/v/nfl_cutout/players_l/20141101/25711.png</url>
   <size>small</size>
  </headshot>
  <image_url>http://l.yimg.com/iu/api/res/1.2/sm2sm277cd6oNXrZU.uiSQ--/YXBwaWQ9c2hhcmVkO2NoPTIzMzY7Y3I9MTtjdz0xNzkwO2R4PTg1NztkeT0wO2ZpPXVsY3JvcDtoPTYwO3E9MTAwO3c9NDY-/https://s.yimg.com/xe/i/us/sp/v/nfl_cutout/players_l/20141101/25711.png</image_url>
  <is_undroppable>0</is_undroppable>
  <position_type>O</position_type>
  <eligible_positions>
   <position>QB</position>
  </eligible_positions>
  <player_stats>
   <coverage_type>season</coverage_type>
   <season>2015</season>
   <stats>
    <stat>
     <stat_id>0</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>1</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>2</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>3</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>4</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>5</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>6</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>7</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>8</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>9</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>10</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>11</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>12</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>13</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>14</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>15</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>16</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>17</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>18</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>57</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>58</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>59</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>60</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>61</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>62</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>63</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>64</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>78</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>79</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>80</stat_id>
     <value>0</value>
    </stat>
    <stat>
     <stat_id>81</stat_id>
     <value>0</value>
    </stat>
   </stats>
  </player_stats>
 </player>
</fantasy_content>

下面是源代码:

import requests
import sys
import time
import webbrowser
import requests
import xml.etree.ElementTree as ET
from oauth_hook import OAuthHook
from requests_oauthlib import OAuth1Session
from requests_oauthlib import OAuth1
from urlparse import parse_qs
from yahoo_oauth import OAuth1

file = open("data.txt",'w')

oauth = OAuth1(None,None,from_file="oauth1.json")

if not oauth.token_is_valid():
    print 'not valid'
    oauth.refresh_access_token()

response = oauth.session.get(url)
root = ET.fromstring(response.text.encode('utf-8'))
print root[0][1].text#prints out 25711, the player ID of Andrew Luck

最佳答案

您的 XML 在根元素处声明了默认命名空间:

xmlns="http://fantasysports.yahooapis.com/fantasy/v2/base.rng"

后代元素隐式继承祖先默认命名空间,除非另有指定。要使用 xml.etree.ElementTree 查找命名空间中的元素,您可以创建前缀到命名空间-uri 的映射并在 XPath 表达式中使用前缀:

ns = {'d': 'http://fantasysports.yahooapis.com/fantasy/v2/base.rng'} 
result = root.find('.//d:player_id', ns)
print(result.text)

或者直接使用命名空间-uri,如下所示:

result = root.find('.//{http://fantasysports.yahooapis.com/fantasy/v2/base.rng}player_id')
print(result.text)

关于python - 雅虎体育 API XML 命名空间找不到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32494573/

相关文章:

python - 多RTSP接收方法

Python 正则表达式在与 re.findall 一起使用时返回匹配的一部分

javascript - 为什么我的 Google 应用程序脚本出现系统标识符错误

ios - NSXMLParser 应用终止 "unrecognized selector sent to instance"

c# - 如何在 C# 中返回空 XML(类型 XmlElement)?

Java:我可以在枚举中使用两个不同的名称来算作同一事物吗?

python - 在多个时间范围内聚合/重采样 pandas 多索引数据帧并预测 ARIMA

c++ - 实现或定义在未命名/匿名命名空间内声明的类或函数

vb.net 仅限外部的属性或方法

python - GStreamer Python 解码器,jpegenc 元素未链接