<p>我在两个json文件上收集数据。你知道吗</p>
<p>第一个有一些我可以收集的数据。你知道吗</p>
<p>第二个没有所需的数据。我想把“娜”储存起来。你知道吗</p>
<p>我的问题是我不知道如何在脚本中正确地存储我的'NA'。你知道吗</p>
<p>这是我的密码:</p>
<pre><code>import requests
# this is our profile ids
profile=['kaid_896965538702696832878421','kaid_1143236333220233567674383']
# prepare the list to get data
badgechall=[]
# do this for each profile id
for kaid in profile:
# request the api link of the profile
data = requests.get('https://www.khanacademy.org/api/internal/user/{}/profile/widgets?lang=en&_=190424-1429-bcf153233dc9_1556201931959'.format(kaid)).json()
# go through each json file to get the data
for item in data:
# try to find on each dictionary of the list the desired data or pass
try:
for badges in item['renderData']['badgeCountData']['counts']:
if badges['typeLabel'] == 'Challenge Patches':
badgechall.append(badges['count'])
except KeyError:
pass
print(badgechall)
</code></pre>
<p>当我运行这个代码时,我得到:
<code>[100]</code></p>
<p>我想得到的是:
<code>[100, 'NA']</code></p>
<p><code>'100'</code>对应于第一个配置文件<code>'kaid_896965538702696832878421'</code>和<code>'NA'</code>对应于第二个配置文件<code>'kaid_1143236333220233567674383'</code>。你知道吗</p>
<p>我想有第一和第二个链接的数据,如果没有返回<code>'NA'</code>。所以我们应该有一个只有2个值的列表。你知道吗</p>
<p>我试过:</p>
<pre><code>except KeyError:
badgechall.append('NA')
pass
</code></pre>
<p>但它的回报是:
<code>[100, 'NA', 'NA', 'NA', 'NA', 'NA', 'NA', 'NA']</code></p>