gpt4 book ai didi

python - 从请求到多事件 api 获取空数组

转载 作者:太空宇宙 更新时间:2023-11-03 11:18:58 25 4
gpt4 key购买 nike

我正在尝试使用多事件 api 来获取有关两个日期之间的音乐事件(音乐会)的信息。例如,我想获取 20171012 到 20171013 期间每场音乐会的以下信息:

- city
- performer
- country
- latitude
- longitude
- genre
- title
- image
- StarTime

我使用在线提供的 python 示例并对其进行更改以获取上述数据。但是现在它不起作用我只能得到这些信息:

 {'latitude': '40.4', 
'longitude': '-3.68333',
'start_time': '2017-10-12 20:00:00',
'city_name': 'Madrid', 'title': 'Kim Waters & Maysa Smooth en Hot Jazz Festival'}

但表演者、流派国家和图片网址无效。你知道如何获得这些信息吗?当我更改下面的 python 示例以获取此信息时,它始终返回一个空数组。

python 示例工作:(但是,如果我将这些元素添加到 event_features,我会得到一个空数组,但没有获得表演者、流派、国家和图像 url)

   import requests
import datetime

def get_event(user_key, event_location , start_date, end_date, event_features, fname):

data_lst = [] # output
start_year = int(start_date[0:4])
start_month = int(start_date[4:6])
start_day = int(start_date[6:])

end_year = int(end_date[0:4])
end_month = int(end_date[4:6])
end_day = int(end_date[6:])

start_date = datetime.date(start_year, start_month, start_day)
end_date = datetime.date(end_year, end_month, end_day)
step = datetime.timedelta(days=1)

while start_date <= end_date:

date = str(start_date.year)
if start_date.month < 10:
date += '0' + str(start_date.month)
else:
date += str(start_date.month)

if start_date.day < 10:
date += '0' + str(start_date.day)
else:
date += str(start_date.day)
date += "00"
date += "-" + date

url = "http://api.eventful.com/json/events/search?"
url += "&app_key=" + user_key
url += "&location=" + event_location
url += "&date=" + date
url += "&page_size=250"
url += "&sort_order=popularity"
url += "&sort_direction=descending"
url += "&q=music"
url+= "&c=music"

data = requests.get(url).json()

try:
for i in range(len(data["events"]["event"])):
data_dict = {}
for feature in event_features:
data_dict[feature] = data["events"]["event"][i][feature]
data_lst.append(data_dict)
except:
pass

print(data_lst)
start_date += step


def main():

user_key = ""
event_location = "Madrid"
start_date = "20171012"
end_date = "20171013"
event_location = event_location.replace("-", " ")
start_date = start_date
end_date = end_date
event_features = ["latitude", "longitude", "start_time"]
event_features += ["city_name", "title"]
event_fname = "events.csv"

get_event(user_key, event_location, start_date, end_date, event_features, event_fname)


if __name__ == '__main__':
main()

最佳答案

您应该调试您的问题并且不要忽略所有异常

替换行 try: ... except: pass by:

    data = requests.get(url).json()
if "event" in data.get("event", {}):
for row in data["events"]["event"]:
# print(row) # you can look here what are the available data, while debugging
data_dict = {feature: row[feature] for feature in features}
data_lst.append(data_dict)
else:
pass # a problem - you can do something here

您将看到一个 KeyError,其中包含缺少的 feature 的名称,该名称在“行”中不存在。您应该修复缺失的功能并阅读有关该服务的 API 的文档。国家特征可能是类似于“city_name”的“country_name”。也许您应该设置“include”参数以在搜索中指定比默认值更多的详细信息部分。


一个通用的 try: ... except: pass 不应该被使用,因为“错误不应该悄悄地过去。” ( The Zen of Python )

阅读 Handling Exceptions :

... The last except clause may omit the exception name(s), to serve as a wildcard. Use this with extreme caution, since it is easy to mask a real programming error in this way! ...

一个更重要的可能会出现意外异常的命令是requests.get(url).json(),例如超时异常。无论如何,如果出现问题,您不应该继续“while”循环。

关于python - 从请求到多事件 api 获取空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46708267/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com