1

我正在尝试报废一个网站,报废的结果是我按预期得到了一个字典列表,但是当我尝试过滤字典时出现错误。当我使用 BeautifulSoup 时,我得到一个错误,但是当我将字典输出复制并粘贴到一个变量中时,我没有得到任何错误。如果我在这个问题中使用了错误的术语,我提前道歉。我是编程和堆栈溢出的新手

import urllib2
from BeautifulSoup import  BeautifulSoup
url = ("http://mlb.mlb.com/lookup/json/named.sport_hitting_composed.bam?game_type='R'&sport_code='mlb'&sport_code='aaa'&sport_code='aax'&sport_code='afa'&sport_code='afx'&sport_code='asx'&sport_code='rok'&sort_by='season_asc'&player_id=405395&sport_hitting_composed.season=2013")
p = urllib2.urlopen(url)
soup = BeautifulSoup(p)

我得到的结果如下:

{"sport_hitting_composed":{"copyRight":" Copyright 2013 MLB Advanced Media, L.P.  Use of any content on this page acknowledges agreement to the terms posted here http://gdx.mlb.com/components/copyright.txt  ","sport_career_hitting":{"queryResults":{"created":"2013-01-27T15:53:40","totalSize":"4","row":[{"hr":"475","gidp":"251","sac":"1","team_count":"2","sport_code":"mlb","rbi":"1434","tb":"4206","sport_id":"1","bb":"1027","avg":".325","slg":".608","ops":"1.022","hbp":"82","g":"1859","d":"505","so":"780","sport":"MLB","sf":"74","h":"2246","cs":"36","obp":".414","t":"15","ao":"2089","r":"1376","go_ao":"0.95","sb":"92","player_id":"405395","ab":"6919","ibb":"267","go":"1988"},{"hr":"0","gidp":"0","sac":"0","team_count":"1","sport_code":"aaa","rbi":"2","tb":"4","sport_id":"11","bb":"1","avg":".214","slg":".286","ops":".552","hbp":"0","g":"3","d":"1","so":"2","sport":"AAA","sf":"0","h":"3","cs":"0","obp":".267","t":"0","ao":"0","r":"1","go_ao":"","sb":"1","player_id":"405395","ab":"14","ibb":"0","go":""},{"hr":"2","gidp":"3","sac":"1","team_count":"1","sport_code":"afa","rbi":"10","tb":"39","sport_id":"13","bb":"7","avg":".284","slg":".481","ops":".822","hbp":"0","g":"21","d":"8","so":"8","sport":"A (Adv)","sf":"0","h":"23","cs":"1","obp":".341","t":"1","ao":"0","r":"11","go_ao":"","sb":"1","player_id":"405395","ab":"81","ibb":"0","go":""},{"hr":"17","gidp":"10","sac":"0","team_count":"1","sport_code":"afx","rbi":"84","tb":"223","sport_id":"14","bb":"38","avg":".324","slg":".565","ops":".953","hbp":"5","g":"109","d":"32","so":"37","sport":"A (Full)","sf":"2","h":"128","cs":"4","obp":".389","t":"6","ao":"0","r":"62","go_ao":"","sb":"2","player_id":"405395","ab":"395","ibb":"7","go":""}]}},"sport_hitting_tm":{"queryResults":{"created":"2013-01-27T15:53:40","totalSize":"15","row":[{"hr":"0","gidp":"0","sac":"0","sport_code":"aaa","rbi":"2","tb":"4","sport_id":"11","slg":".286","bb":"1","avg":".214","league_short":"Pacific Coast","ops":".552","team_full":"Memphis Redbirds","hbp":"0","g":"3","league_full":"Pacific Coast League","d":"1","team_abbrev":"MEM","so":"2","sport":"AAA","league_id":"112","team_short":"Memphis","sf":"0","team_seq":"1","league":"PCL","h":"3","cs":"0","obp":".267","t":"0","ao":"0","season":"2000","r":"1","go_ao":"","sb":"1","player_id":"405395","ab":"14","ibb":"0","team_id":"235","go":""},{"hr":"2","gidp":"3","sac":"1","sport_code":"afa","rbi":"10","tb":"39","sport_id":"13","slg":".481","bb":"7","avg":".284","league_short":"Carolina","ops":".822","team_full":"Potomac Cannons","hbp":"0","g":"21","league_full":"Carolina League","d":"8","team_abbrev":"POT","so":"8","sport":"A (Adv)","league_id":"122","team_short":"Potomac","sf":"0","team_seq":"2","league":"CAR","h":"23","cs":"1","obp":".341","t":"1","ao":"0","season":"2000","r":"11","go_ao":"","sb":"1","player_id":"405395","ab":"81","ibb":"0","team_id":"436","go":""},{"hr":"17","gidp":"10","sac":"0","sport_code":"afx","rbi":"84","tb":"223","sport_id":"14","slg":".565","bb":"38","avg":".324","league_short":"Midwest","ops":".953","team_full":"Peoria Chiefs","hbp":"5","g":"109","league_full":"Midwest League","d":"32","team_abbrev":"PEO","so":"37","sport":"A (Full)","league_id":"118","team_short":"Peoria","sf":"2","team_seq":"3","league":"MID","h":"128","cs":"4","obp":".389","t":"6","ao":"0","season":"2000","r":"62","go_ao":"","sb":"2","player_id":"405395","ab":"395","ibb":"7","team_id":"443","go":""},{"hr":"37","gidp":"21","sac":"1","sport_code":"mlb","rbi":"130","tb":"360","sport_id":"1","slg":".610","bb":"69","avg":".329","league_short":"National","ops":"1.013","team_full":"St. Louis Cardinals","hbp":"9","g":"161","league_full":"National League","d":"47","team_abbrev":"STL","so":"93","sport":"MLB","league_id":"104","team_short":"St. Louis","sf":"7","team_seq":"1","league":"NL","h":"194","cs":"3","obp":".403","t":"4","ao":"162","season":"2001","r":"112","go_ao":"0.92","sb":"1","player_id":"405395","ab":"590","ibb":"6","team_id":"138","go":"149"},{"hr":"34","gidp":"20","sac":"0","sport_code":"mlb","rbi":"127","tb":"331","sport_id":"1","slg":".561","bb":"72","avg":".314","league_short":"National","ops":".955","team_full":"St. Louis Cardinals","hbp":"9","g":"157","league_full":"National League","d":"40","team_abbrev":"STL","so":"69","sport":"MLB","league_id":"104","team_short":"St. Louis","sf":"4","team_seq":"1","league":"NL","h":"185","cs":"4","obp":".394","t":"2","ao":"163","season":"2002","r":"118","go_ao":"1.09","sb":"2","player_id":"405395","ab":"590","ibb":"13","team_id":"138","go":"177"},{"hr":"43","gidp":"13","sac":"0","sport_code":"mlb","rbi":"124","tb":"394","sport_id":"1","slg":".667","bb":"79","avg":".359","league_short":"National","ops":"1.106","team_full":"St. Louis Cardinals","hbp":"10","g":"157","league_full":"National League","d":"51","team_abbrev":"STL","so":"65","sport":"MLB","league_id":"104","team_short":"St. Louis","sf":"5","team_seq":"1","league":"NL","h":"212","cs":"1","obp":".439","t":"1","ao":"158","season":"2003","r":"137","go_ao":"1.02","sb":"5","player_id":"405395","ab":"591","ibb":"12","team_id":"138","go":"161"},{"hr":"46","gidp":"21","sac":"0","sport_code":"mlb","rbi":"123","tb":"389","sport_id":"1","slg":".657","bb":"84","avg":".331","league_short":"National","ops":"1.072","team_full":"St. Louis Cardinals","hbp":"7","g":"154","league_full":"National League","d":"51","team_abbrev":"STL","so":"52","sport":"MLB","league_id":"104","team_short":"St. Louis","sf":"9","team_seq":"1","league":"NL","h":"196","cs":"5","obp":".415","t":"2","ao":"183","season":"2004","r":"133","go_ao":"0.93","sb":"5","player_id":"405395","ab":"592","ibb":"12","team_id":"138","go":"170"},{"hr":"41","gidp":"19","sac":"0","sport_code":"mlb","rbi":"117","tb":"360","sport_id":"1","slg":".609","bb":"97","avg":".330","league_short":"National","ops":"1.039","team_full":"St. Louis Cardinals","hbp":"9","g":"161","league_full":"National League","d":"38","team_abbrev":"STL","so":"65","sport":"MLB","league_id":"104","team_short":"St. Louis","sf":"3","team_seq":"1","league":"NL","h":"195","cs":"2","obp":".430","t":"2","ao":"176","season":"2005","r":"129","go_ao":"0.90","sb":"16","player_id":"405395","ab":"591","ibb":"27","team_id":"138","go":"158"},{"hr":"49","gidp":"20","sac":"0","sport_code":"mlb","rbi":"137","tb":"359","sport_id":"1","slg":".671","bb":"92","avg":".331","league_short":"National","ops":"1.102","team_full":"St. Louis Cardinals","hbp":"4","g":"143","league_full":"National League","d":"33","team_abbrev":"STL","so":"50","sport":"MLB","league_id":"104","team_short":"St. Louis","sf":"3","team_seq":"1","league":"NL","h":"177","cs":"2","obp":".431","t":"1","ao":"178","season":"2006","r":"119","go_ao":"0.75","sb":"7","player_id":"405395","ab":"535","ibb":"28","team_id":"138","go":"133"},{"hr":"32","gidp":"27","sac":"0","sport_code":"mlb","rbi":"103","tb":"321","sport_id":"1","slg":".568","bb":"99","avg":".327","league_short":"National","ops":".997","team_full":"St. Louis Cardinals","hbp":"7","g":"158","league_full":"National League","d":"38","team_abbrev":"STL","so":"58","sport":"MLB","league_id":"104","team_short":"St. Louis","sf":"8","team_seq":"1","league":"NL","h":"185","cs":"6","obp":".429","t":"1","ao":"167","season":"2007","r":"99","go_ao":"0.98","sb":"2","player_id":"405395","ab":"565","ibb":"22","team_id":"138","go":"163"},{"hr":"37","gidp":"16","sac":"0","sport_code":"mlb","rbi":"116","tb":"342","sport_id":"1","slg":".653","bb":"104","avg":".357","league_short":"National","ops":"1.114","team_full":"St. Louis Cardinals","hbp":"5","g":"148","league_full":"National League","d":"44","team_abbrev":"STL","so":"54","sport":"MLB","league_id":"104","team_short":"St. Louis","sf":"8","team_seq":"1","league":"NL","h":"187","cs":"3","obp":".462","t":"0","ao":"159","season":"2008","r":"100","go_ao":"0.92","sb":"7","player_id":"405395","ab":"524","ibb":"34","team_id":"138","go":"147"},{"hr":"47","gidp":"23","sac":"0","sport_code":"mlb","rbi":"135","tb":"374","sport_id":"1","slg":".658","bb":"115","avg":".327","league_short":"National","ops":"1.101","team_full":"St. Louis Cardinals","hbp":"9","g":"160","league_full":"National League","d":"45","team_abbrev":"STL","so":"64","sport":"MLB","league_id":"104","team_short":"St. Louis","sf":"8","team_seq":"1","league":"NL","h":"186","cs":"4","obp":".443","t":"1","ao":"187","season":"2009","r":"124","go_ao":"0.87","sb":"16","player_id":"405395","ab":"568","ibb":"44","team_id":"138","go":"162"},{"hr":"42","gidp":"23","sac":"0","sport_code":"mlb","rbi":"118","tb":"350","sport_id":"1","slg":".596","bb":"103","avg":".312","league_short":"National","ops":"1.011","team_full":"St. Louis Cardinals","hbp":"4","g":"159","league_full":"National League","d":"39","team_abbrev":"STL","so":"76","sport":"MLB","league_id":"104","team_short":"St. Louis","sf":"6","team_seq":"1","league":"NL","h":"183","cs":"4","obp":".414","t":"1","ao":"189","season":"2010","r":"115","go_ao":"0.89","sb":"14","player_id":"405395","ab":"587","ibb":"38","team_id":"138","go":"168"},{"hr":"37","gidp":"29","sac":"0","sport_code":"mlb","rbi":"99","tb":"313","sport_id":"1","slg":".541","bb":"61","avg":".299","league_short":"National","ops":".906","team_full":"St. Louis Cardinals","hbp":"4","g":"147","league_full":"National League","d":"29","team_abbrev":"STL","so":"58","sport":"MLB","league_id":"104","team_short":"St. Louis","sf":"7","team_seq":"1","league":"NL","h":"173","cs":"1","obp":".366","t":"0","ao":"179","season":"2011","r":"105","go_ao":"1.15","sb":"9","player_id":"405395","ab":"579","ibb":"15","team_id":"138","go":"205"},{"hr":"30","gidp":"19","sac":"0","sport_code":"mlb","rbi":"105","tb":"313","sport_id":"1","slg":".516","bb":"52","avg":".285","league_short":"American","ops":".859","team_full":"Los Angeles Angels","hbp":"5","g":"154","league_full":"American League","d":"50","team_abbrev":"LAA","so":"76","sport":"MLB","league_id":"103","team_short":"LA Angels","sf":"6","team_seq":"1","league":"AL","h":"173","cs":"1","obp":".343","t":"0","ao":"188","season":"2012","r":"85","go_ao":"1.04","sb":"8","player_id":"405395","ab":"607","ibb":"16","team_id":"108","go":"195"}]}},"sport_career_hitting_agg":{"queryResults":{"created":"2013-01-27T15:53:40","totalSize":"2","row":[{"hr":"475","gidp":"251","sac":"1","team_count":"2","sport_code":"mlb","rbi":"1434","tb":"4206","sport_id":"1","bb":"1027","avg":".325","slg":".608","ops":"1.022","hbp":"82","g":"1859","d":"505","so":"780","sport":"MLB","sf":"74","h":"2246","cs":"36","obp":".414","t":"15","ao":"2089","r":"1376","go_ao":"0.95","sb":"92","player_id":"405395","ab":"6919","ibb":"267","go":"1988"},{"hr":"19","gidp":"13","sac":"1","team_count":"3","sport_code":"min","rbi":"96","tb":"266","sport_id":"21","bb":"46","avg":".314","slg":".543","ops":".920","hbp":"5","g":"133","d":"41","so":"47","sport":"Minors","sf":"2","h":"154","cs":"5","obp":".378","t":"7","ao":"0","r":"74","go_ao":"","sb":"4","player_id":"405395","ab":"490","ibb":"7","go":""}]}},"sport_hitting_agg":{"queryResults":{"created":"2013-01-27T15:53:40","totalSize":"13","row":[{"hr":"19","gidp":"13","sac":"1","team_count":"3","sport_code":"min","rbi":"96","tb":"266","sport_id":"21","bb":"46","avg":".314","slg":".543","ops":".920","hbp":"5","g":"133","d":"41","so":"47","sport":"Minors","sf":"2","h":"154","cs":"5","obp":".378","t":"7","ao":"0","season":"2000","r":"74","go_ao":"","sb":"4","player_id":"405395","ab":"490","ibb":"7","go":""},{"hr":"37","gidp":"21","sac":"1","team_count":"1","sport_code":"mlb","rbi":"130","tb":"360","sport_id":"1","bb":"69","avg":".329","slg":".610","ops":"1.013","hbp":"9","g":"161","d":"47","so":"93","sport":"MLB","sf":"7","h":"194","cs":"3","obp":".403","t":"4","ao":"162","season":"2001","r":"112","go_ao":"0.92","sb":"1","player_id":"405395","ab":"590","ibb":"6","go":"149"},{"hr":"34","gidp":"20","sac":"0","team_count":"1","sport_code":"mlb","rbi":"127","tb":"331","sport_id":"1","bb":"72","avg":".314","slg":".561","ops":".955","hbp":"9","g":"157","d":"40","so":"69","sport":"MLB","sf":"4","h":"185","cs":"4","obp":".394","t":"2","ao":"163","season":"2002","r":"118","go_ao":"1.09","sb":"2","player_id":"405395","ab":"590","ibb":"13","go":"177"},{"hr":"43","gidp":"13","sac":"0","team_count":"1","sport_code":"mlb","rbi":"124","tb":"394","sport_id":"1","bb":"79","avg":".359","slg":".667","ops":"1.106","hbp":"10","g":"157","d":"51","so":"65","sport":"MLB","sf":"5","h":"212","cs":"1","obp":".439","t":"1","ao":"158","season":"2003","r":"137","go_ao":"1.02","sb":"5","player_id":"405395","ab":"591","ibb":"12","go":"161"},{"hr":"46","gidp":"21","sac":"0","team_count":"1","sport_code":"mlb","rbi":"123","tb":"389","sport_id":"1","bb":"84","avg":".331","slg":".657","ops":"1.072","hbp":"7","g":"154","d":"51","so":"52","sport":"MLB","sf":"9","h":"196","cs":"5","obp":".415","t":"2","ao":"183","season":"2004","r":"133","go_ao":"0.93","sb":"5","player_id":"405395","ab":"592","ibb":"12","go":"170"},{"hr":"41","gidp":"19","sac":"0","team_count":"1","sport_code":"mlb","rbi":"117","tb":"360","sport_id":"1","bb":"97","avg":".330","slg":".609","ops":"1.039","hbp":"9","g":"161","d":"38","so":"65","sport":"MLB","sf":"3","h":"195","cs":"2","obp":".430","t":"2","ao":"176","season":"2005","r":"129","go_ao":"0.90","sb":"16","player_id":"405395","ab":"591","ibb":"27","go":"158"},{"hr":"49","gidp":"20","sac":"0","team_count":"1","sport_code":"mlb","rbi":"137","tb":"359","sport_id":"1","bb":"92","avg":".331","slg":".671","ops":"1.102","hbp":"4","g":"143","d":"33","so":"50","sport":"MLB","sf":"3","h":"177","cs":"2","obp":".431","t":"1","ao":"178","season":"2006","r":"119","go_ao":"0.75","sb":"7","player_id":"405395","ab":"535","ibb":"28","go":"133"},{"hr":"32","gidp":"27","sac":"0","team_count":"1","sport_code":"mlb","rbi":"103","tb":"321","sport_id":"1","bb":"99","avg":".327","slg":".568","ops":".997","hbp":"7","g":"158","d":"38","so":"58","sport":"MLB","sf":"8","h":"185","cs":"6","obp":".429","t":"1","ao":"167","season":"2007","r":"99","go_ao":"0.98","sb":"2","player_id":"405395","ab":"565","ibb":"22","go":"163"},{"hr":"37","gidp":"16","sac":"0","team_count":"1","sport_code":"mlb","rbi":"116","tb":"342","sport_id":"1","bb":"104","avg":".357","slg":".653","ops":"1.114","hbp":"5","g":"148","d":"44","so":"54","sport":"MLB","sf":"8","h":"187","cs":"3","obp":".462","t":"0","ao":"159","season":"2008","r":"100","go_ao":"0.92","sb":"7","player_id":"405395","ab":"524","ibb":"34","go":"147"},{"hr":"47","gidp":"23","sac":"0","team_count":"1","sport_code":"mlb","rbi":"135","tb":"374","sport_id":"1","bb":"115","avg":".327","slg":".658","ops":"1.101","hbp":"9","g":"160","d":"45","so":"64","sport":"MLB","sf":"8","h":"186","cs":"4","obp":".443","t":"1","ao":"187","season":"2009","r":"124","go_ao":"0.87","sb":"16","player_id":"405395","ab":"568","ibb":"44","go":"162"},{"hr":"42","gidp":"23","sac":"0","team_count":"1","sport_code":"mlb","rbi":"118","tb":"350","sport_id":"1","bb":"103","avg":".312","slg":".596","ops":"1.011","hbp":"4","g":"159","d":"39","so":"76","sport":"MLB","sf":"6","h":"183","cs":"4","obp":".414","t":"1","ao":"189","season":"2010","r":"115","go_ao":"0.89","sb":"14","player_id":"405395","ab":"587","ibb":"38","go":"168"},{"hr":"37","gidp":"29","sac":"0","team_count":"1","sport_code":"mlb","rbi":"99","tb":"313","sport_id":"1","bb":"61","avg":".299","slg":".541","ops":".906","hbp":"4","g":"147","d":"29","so":"58","sport":"MLB","sf":"7","h":"173","cs":"1","obp":".366","t":"0","ao":"179","season":"2011","r":"105","go_ao":"1.15","sb":"9","player_id":"405395","ab":"579","ibb":"15","go":"205"},{"hr":"30","gidp":"19","sac":"0","team_count":"1","sport_code":"mlb","rbi":"105","tb":"313","sport_id":"1","bb":"52","avg":".285","slg":".516","ops":".859","hbp":"5","g":"154","d":"50","so":"76","sport":"MLB","sf":"6","h":"173","cs":"1","obp":".343","t":"0","ao":"188","season":"2012","r":"85","go_ao":"1.04","sb":"8","player_id":"405395","ab":"607","ibb":"16","go":"195"}]}}}}

所以我得到了一个字典列表。如果我尝试过滤列表,则会收到错误消息:

soup['sport_hitting_composed']    
Traceback (most recent call last):                                                                                                                                                                   
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.8-intel/egg/BeautifulSoup.py", line 613, in __getitem__
KeyError: 'sport_hitting_composed'

如果我复制并粘贴整个汤输出,让我们说一个变量 p,我可以过滤列表。

4

1 回答 1

1

你得到的不是字典也不是列表,而是一个json对象。要获得 python 数据结构,您必须先对其进行解码。为此,有一个内置的json 模块

import urllib2
import json
url = ("http://mlb.mlb.com/lookup/json/named.sport_hitting_composed.bam?game_type='R'&sport_code='mlb'&sport_code='aaa'&sport_code='aax'&sport_code='afa'&sport_code='afx'&sport_code='asx'&sport_code='rok'&sort_by='season_asc'&player_id=405395&sport_hitting_composed.season=2013")
res = urllib2.urlopen(url)
data = json.load(res)  # type(data) --> dict
print data[u'sport_hitting_composed']
于 2013-01-27T22:15:14.037 回答