因此,在使用 Yahoo 的 Sports API 进行查询后,我开始尝试在 python 中使用 lxml 解析 XML:我尝试在将元素存储在根目录后获取元素,如果我简单地使用索引,我可以访问子标签;但是调用类似 root.find("season") 的东西不会返回任何内容。根据我读过的其他帖子,我相信这可能与 xml 文件中使用的命名空间有关。我应该如何处理这个问题,以便我可以获得我需要的元素?
编辑:我很抱歉不够清楚。我正在尝试看看 NFL QB Andrew Luck。代码访问 root[0][1]。root[0] 应该是 player 标签,但是 player 标签里面有很多孩子,所以 root[0][0] 是它的第一个孩子,即 player_key 标签,而 root[0][1] 是 player 标签的第二个孩子,这应该是 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