家庭作业是Watson中的python笔记本项目。作业为函数get_basketball_stats(link =“ ...”)提供了以下代码。但是,它返回错误的结果:字典的值和键不匹配,即键“ PPG”被赋予“ GP”的值。
我在Google Colab中尝试了相同的代码。结果是正确的。 Google colab python版本是3.6.7。我怀疑Watson(3.5.5)中过时的python版本会导致错误的字典,因此我在这里问一个问题:如何升级Watson的python版本?
def get_basketball_stats(link='https://en.wikipedia.org/wiki/Michael_Jordan'):
# read the webpage
response = requests.get(link)
# create a BeautifulSoup object to parse the HTML
soup = bs4.BeautifulSoup(response.text, 'html.parser')
# the player stats are defined with the attribute CSS class set to 'wikitable sortable';
#therefore we create a tag object "table"
table=soup.find(class_='wikitable sortable')
#表格的标题是表格的第一行(tr),我们创建的标签对象具有第一行
标头= table.tr
#表列名称显示为缩写;因此,我们找到了所有的abbr标签,并返回了一个Iterator
title = headers.find_all(“ abbr”)
#我们创建字典并将表头作为键传递
数据= {title ['title']:[]标题中的标题}
#我们将每列存储为字典中的列表,该列的标题为字典键
#we iterate over each table row by fining each table tag tr and assign it to the objed
for row in table.find_all('tr')[1:]:
#we iterate over each cell in the table, as each cell corresponds to a different column we all obtain the correspondin key corresponding the column n
for key,a in zip(data.keys(),row.find_all("td")[2:]):
# we append each elment and strip any extra HTML contnet
data[key].append(''.join(c for c in a.text if (c.isdigit() or c == ".")))
# we remove extra rows by finding the smallest list
Min=min([len(x) for x in data.values()])
#we convert the elements in the key to floats
for key in data.keys():
data[key]=list(map(lambda x: float(x), data[key][:Min]))
return data
我希望这些键能够像Google Colad一样在Watson中匹配其对应的值。