Yahoo Sports API XML命名空间无法找到元素

时间:2015-09-10 06:23:34

标签: python xml namespaces elementtree default-namespace

因此在使用Yahoo的Sports API进行查询后,我开始尝试使用python中的lxml解析XML:我已经尝试在将它们存储在根目录中后获取元素,如果我只是使用它,我可以访问子标记索引;但是调用root.find(“season”)这样的东西什么也不返回。基于我读过的其他帖子,我相信这可能与xml文件中使用的命名空间有关。 我应该如何处理这个问题,以便获得我需要的元素?

编辑:我很抱歉不够清楚。我试图看看NFL QB 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

0 个答案:

没有答案