首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python路径不存在

Python路径不存在
EN

Stack Overflow用户
提问于 2022-02-02 09:20:11
回答 3查看 184关注 0票数 0

下面的链接返回Json数据。我从几个元素中获取数据,有时数据点是不可用的,映射也不存在。如果item不存在,我希望将我的变量设置为空,这样它就可以继续,而不是错误地退出和停止。

代码语言:javascript
运行
复制
import requests
import pandas as pd
import json

gameId = 220808023
url = 'http://site.api.espn.com/apis/site/v2/sports/football/nfl/summary'
payload = {'event':gameId}

jsonData = requests.get(url,headers = {'User-agent': 'Chrome/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0'}, params=payload).json()

Venue = [jsonData['gameInfo']['venue']['id']]

全链路

http://site.api.espn.com/apis/site/v2/sports/football/nfl/summary?event=220808023

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-02-02 13:44:09

我会做两件事中的一件:

  1. 检查该键是否存在于嵌套字典节点
  2. 中,使用try/除

这两个选项都在下面。使用try/ why的额外好处是,您可以记录错误,以查看为什么将地点存储为"N/A“。

1

代码语言:javascript
运行
复制
import requests
import pandas as pd

gameId = 220808023
url = 'https://site.api.espn.com/apis/site/v2/sports/football/nfl/summary'
payload = {'event':gameId}

jsonData = requests.get(url,headers = {'User-agent': 'Chrome/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0'}, params=payload).json()

if 'venue' in jsonData['gameInfo'].keys():
    venue = [jsonData['gameInfo']['venue']['id']]
else:
    venue = 'N/A'

2

代码语言:javascript
运行
复制
import requests
import pandas as pd    
    
gameId = 220808023
url = 'https://site.api.espn.com/apis/site/v2/sports/football/nfl/summary'
payload = {'event':gameId}

jsonData = requests.get(url,headers = {'User-agent': 'Chrome/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0'}, params=payload).json()

try:
    venue = [jsonData['gameInfo']['venue']['id']]
except Exception as e:
    print(type(e).__name__, e)
    venue = 'N/A'

输出:

代码语言:javascript
运行
复制
print(venue)
N/A

你的另一个选择是从另一个来源获取信息,使用nfl赛季和主队。我只是使用维基百科,因为这是一个快速抢占团队和他们的主场按季节。但可能有一个更好的来源:

代码语言:javascript
运行
复制
import requests
import pandas as pd

gameId = 220808023
url = 'https://site.api.espn.com/apis/site/v2/sports/football/nfl/summary'
payload = {'event':gameId}

jsonData = requests.get(url,headers = {'User-agent': 'Chrome/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0'}, params=payload).json()

if 'venue' in jsonData['gameInfo'].keys():
    venue = [jsonData['gameInfo']['venue']['id']]
else:
    print('Venue no available on ESPN.\nSearching other source...')
    nfl_season = jsonData['header']['season']['year']
    for alpha in jsonData['header']['competitions'][0]['competitors']:
        if alpha['homeAway'] == 'home':
            homeTeam = alpha['team']['location'] + ' ' +  alpha ['team']['name']

            break
        
    listNflStadiums = pd.read_html('https://en.wikipedia.org/wiki/Chronology_of_home_stadiums_for_current_National_Football_League_teams')[0]
    teamStadiums = listNflStadiums[listNflStadiums[0].str.contains(homeTeam)]
    for idx, row in teamStadiums.iterrows():
        if nfl_season > int(row[4]):
            venue = row[1].split('(')[0].strip()
            break
    
    
    print(venue)

输出:

代码语言:javascript
运行
复制
Venue no available on ESPN.
Searching other source...
Heinz Field
票数 1
EN

Stack Overflow用户

发布于 2022-02-02 09:33:35

您的数据不包含您要查找的内容。

代码语言:javascript
运行
复制
import json

import requests

gameId = 220808023
url = 'http://site.api.espn.com/apis/site/v2/sports/football/nfl/summary'
payload = {'event': gameId}

jsonData = requests.get(
    url,
    headers={'User-agent': 'Chrome/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0'},
    params=payload
).json()

# This simple print would have saved you from investigation:

print(json.dumps(jsonData, indent=4))

# > jsonData['gameInfo'] = {
# >     "attendance": 0
# > }

# This is a better way to try if a key exist in your response
try:
    gameInfo = jsonData['gameInfo']
    try:
        venue = gameInfo['venue']
        try:
            id = venue['id']
            
            Venue = id
        except KeyError:
            print('venue data doesnt contains "id"')
    except KeyError:
        print('gameInfo data doesnt contains "venue"')
except KeyError:
    print('json data doesnt contains "gameInfo"')

记住,使用PEP8样式是一种很好的实践,PEP8推荐Snake https://en.wikipedia.org/wiki/Snake_case

票数 1
EN

Stack Overflow用户

发布于 2022-02-02 09:57:43

您可以通过key in dic检查字典是否有特定的密钥。因此,原则上,您可以通过在查询字典键的值之前检查密钥的存在来避免KeyError

为了避免丑陋的嵌套if语句,您可以定义一个包装类,在查询时检查键的存在。重点在于python,通过dic[key]访问值被转换为调用dic.__getitem__(key)

示例:

代码语言:javascript
运行
复制
import requests
import pandas as pd
import json

gameId = 220808023
url = 'http://site.api.espn.com/apis/site/v2/sports/football/nfl/summary'
payload = {'event': gameId}

jsonData = requests.get(url, headers={
                        'User-agent': 'Chrome/5.0 (X11; Linux x86_64; rv:12.0) Gecko/20100101 Firefox/12.0'}, params=payload).json()


class MaybeDict:
    def __init__(self, val):
        self.val = val

    def __getitem__(self, key):
        if not self.val:
            return MaybeDict(None)
        if not key in self.val:
            return MaybeDict(None)
        return MaybeDict(self.val[key])


response = MaybeDict(jsonData)
print(response["gameInfo"]["venue"]["id"].val)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70953112

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档