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

Yahoo Sports API XML Namespace Cannot find Element

因此,在使用 Yahoo's Sports API 进行查询后,我开始尝试使用 python 中的 lxml 来解析 XML:我已经尝试获取将元素存储在根目录后,如果我只是使用索引,我可以访问 children 标签;但是调用 root.find("season") returns 什么都没有。根据我读过的其他帖子,我认为这可能与 xml 文件中使用的命名空间有关。 我该如何处理,才能获得我需要的元素?

编辑:我很抱歉不够清楚。我正在尝试查看 NFL QB Andrew Luck。该代码访问 root[0][1]。 root[0]应该是player标签,但是player标签里面有很多children,所以root[0][0]是第一个child,player_key标签,root [0][1]是玩家标签的第二个child,应该是player_id标签。

但是调用 root[0].find('player_id') 或 find、iter 方法的任何变体 returns 什么都没有。

再次感谢您的帮助!

下面是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)

或者直接使用 namespace-uri,像这样:

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