2017-04-17 14 views
0

MLB APIに使用されるXMLファイルを解析するためにBeautifulSoup 4(およびパーサーlmxl)を使用しています。 APIは、特定の日の現在のゲームのスコアボードを生成し、特定のタブを認識するために美味しいスープを取得するのに問題があります。美しいスープが最初のタグ(XML)を見つけることができません

たとえば、away_file_codeまたはhome_file_codeに基づいて特定のチームのスコアと名前を抽出しようとしていると、私はtoday's gamesを探しています。我々はトロント・ブルージェイズ対オリオールズを見れば、ゲームスコアボードXMLは次のようになります。

<games year="2017" month="04" day="16" modified_date="2017-04-17T01:42:57Z" next_day_date="2017-04-17"> 
<game id="2017/04/16/balmlb-tormlb-1" venue="Rogers Centre" game_pk="490271" time="1:07" time_date="2017/04/16 1:07" time_date_aw_lg="2017/04/16 1:07" time_date_hm_lg="2017/04/16 1:07" time_zone="ET" ampm="PM" first_pitch_et="" away_time="1:07" away_time_zone="ET" away_ampm="PM" home_time="1:07" home_time_zone="ET" home_ampm="PM" game_type="R" tiebreaker_sw="N" resume_date="" original_date="2017/04/16" time_zone_aw_lg="-4" time_zone_hm_lg="-4" time_aw_lg="1:07" aw_lg_ampm="PM" tz_aw_lg_gen="ET" time_hm_lg="1:07" hm_lg_ampm="PM" tz_hm_lg_gen="ET" venue_id="14" scheduled_innings="9" description="" away_name_abbrev="BAL" home_name_abbrev="TOR" away_code="bal" away_file_code="bal" away_team_id="110" away_team_city="Baltimore" away_team_name="Orioles" away_division="E" away_league_id="103" away_sport_code="mlb" home_code="tor" home_file_code="tor" home_team_id="141" home_team_city="Toronto" home_team_name="Blue Jays" home_division="E" home_league_id="103" home_sport_code="mlb" day="SUN" gameday_sw="P" double_header_sw="N" game_nbr="1" tbd_flag="N" away_games_back="-" home_games_back="6.5" away_games_back_wildcard="" home_games_back_wildcard="5.5" venue_w_chan_loc="CAXX0504" location="Toronto, Canada" gameday="2017_04_16_balmlb_tormlb_1" away_win="8" away_loss="3" home_win="2" home_loss="10" game_data_directory="/components/game/mlb/year_2017/month_04/day_16/gid_2017_04_16_balmlb_tormlb_1" league="AA"> 
<status status="Final" ind="F" reason="" inning="9" top_inning="N" b="0" s="0" o="3" inning_state="" note="" is_perfect_game="N" is_no_hitter="N"/> 
<linescore>...</linescore> 
<home_runs>...</home_runs> 
<winning_pitcher id="605164" last="Bundy" first="Dylan" name_display_roster="Bundy" number="37" era="1.86" wins="2" losses="1"/> 
<losing_pitcher id="457918" last="Happ" first="J.A." name_display_roster="Happ" number="33" era="4.50" wins="0" losses="3"/> 
<save_pitcher id="" last="" first="" number="" name_display_roster="" era="0" wins="0" losses="0" saves="0" svo="0"/> 
<links mlbtv="bam.media.launchPlayer({calendar_event_id:'14-490271-2017-04-16',media_type:'video'})" wrapup="/mlb/gameday/index.jsp?gid=2017_04_16_balmlb_tormlb_1&mode=wrap&c_id=mlb" home_audio="bam.media.launchPlayer({calendar_event_id:'14-490271-2017-04-16',media_type:'audio'})" away_audio="bam.media.launchPlayer({calendar_event_id:'14-490271-2017-04-16',media_type:'audio'})" home_preview="/mlb/gameday/index.jsp?gid=2017_04_16_balmlb_tormlb_1&mode=preview&c_id=mlb" away_preview="/mlb/gameday/index.jsp?gid=2017_04_16_balmlb_tormlb_1&mode=preview&c_id=mlb" preview="/mlb/gameday/index.jsp?gid=2017_04_16_balmlb_tormlb_1&mode=preview&c_id=mlb" tv_station="SNET-1"/> 
<broadcast>...</broadcast> 
<alerts text="Final score in Toronto: Baltimore 11, Toronto 4" brief_text="At TOR: Final - BAL 11, TOR 4" type="status"/> 
<game_media>...</game_media> 
<video_thumbnail>...</video_thumbnail> 
<video_thumbnails>...</video_thumbnails> 
</game> 
<game>...</game> (etc...) 

以下のコードの抜粋である私は、game(ないgames)タグを試してみて、見つけて使用していますそれは属性です。問題は、ゲームをリクエストすると、Noneを返します。しかし、私は問題なしで他のタグを返すことができます。たとえば、statusは完全にうまく動作します。

soup = BeautifulSoup(webpage, 'xml') # webpage is the xml file for today's games 
tags = soup.findAll('game', {'home_file_code': 'tor'}) #supposed to find the tags for the home_file_code matching the home team's abbreviation 
for games in tags: 
    print(games.find('status')['status'] #works without an issue 
    print(games.find('game')['home_file_code'] #throws below error, because games.find('game') is None 

TypeError例外は: 'NoneType' オブジェクトは、私はリスト(print(list(games.children)))のために子供を印刷する場合、それはゲーム以外のすべてを返し、

また添字化ではありません。

最初のタグをつかむことができない理由について、XMLに関して何か不足していますか?私はこれがずっと前から私のために働いていたので、かなり混乱していました。私は何が変わったのかは分かりません。

答えて

0

私はfind関数を誤解しているようです。キーワードのためにキーワードを索引付けして、タグ自体の中で必要な属性を検索することができます。だから、基本的に、私は次のことをやっている必要があります。それは、すでに私たちは見上げたタグ内に存在するため、予想通り今

soup = BeautifulSoup(webpage, 'xml') # webpage is the xml file for today's games 
tags = soup.findAll('game', {'home_file_code': 'tor'}) 
for games in tags: 
    print(games.find('status')['status'] 
    print(games['home_file_code']) 

print(games['home_file_code']home_file_codeがあります。

誰かがもっと徹底的な答えを与えることができると確信していますが、それは私が持っていた基本的な誤解でした。

関連する問題