Yahoo Sports API XML Namespace Cannot find Element

173 Views Asked by At

So after making queries with Yahoo's Sports API, I begin to try and parse the XML using lxml in python: I've tried getting the elements after storing them in the root,and i can access the children tags if i simply use indexing; however calling something like root.find("season") returns nothing. Based on other posts I've read I believe this may have to do with the namespace used in the xml file. How should I deal with this, so that I can acquire the elements I need?

Edit: My apologies for not being clear enough. I'm attempting to look at NFL QB Andrew Luck. The code accesses root[0][1]. root[0] should be the player tag but inside the player tag is many children, so root[0][0] is its first child, the player_key tag, and root[0][1] is the player tag's second child, which should be the player_id tag.

But calling root[0].find('player_id') or any variation of a find, iter method returns nothing.

Thanks again for the help!

Below is the xml code

<?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>

Below is the source code:

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
1

There are 1 best solutions below

0
On BEST ANSWER

Your XML has default namespace declared at the root element :

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

descendant elements inherit ancestor default namespace implicitly, unless otherwise specified. To find element in namespace using xml.etree.ElementTree, you can either create mapping of prefix to namespace-uri and use the prefix in the XPath expression :

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

or use the namespace-uri directly, like so :

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