我试图使用eventful 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示例工作:(但是,如果我没有获取表演者,流派,国家和图片网址,如果我将theese元素添加到event_features我得到一个空数组)
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()
答案 0 :(得分:2)
您应该调试问题,不要忽略所有异常。
将行try: ... except: pass
替换为:
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
您会在&#34;行&#34;中看到KeyError
,其中包含缺少feature
的名称。您应该修复缺少的功能以及阅读有关该服务的API 的文档。国家/地区功能可能是&#34; country_name&#34;类似于&#34; city_name&#34;。也许你应该设置&#34; include&#34;参数,用于指定搜索中的详细信息部分,而不是默认值。
永远不应该使用通用try: ... except: pass
,因为&#34;错误永远不会无声通过。&#34; (The Zen of Python)
阅读 Handling Exceptions :
...最后一个except子句可以省略异常名称,以用作通配符。请谨慎使用,因为以这种方式很容易掩盖真正的编程错误! ...
可能出现意外异常的更重要的命令是requests.get(url).json()
,例如TimeoutException异常。无论如何你不应该继续&#34;而#34;如果出现问题则循环。
答案 1 :(得分:0)
我能够成功从performer
,image
和country
字段的Eventful API中提取数据。但是,我不认为Eventful Search API支持genre
- 我没有看到in their documentation。
要获得country
,我在您的event_features数组中添加了"country_name", "country_abbr"
。这会将这些值添加到生成的JSON中:
'country_abbr': u'ESP',
'country_name': u'Spain'
通过将Performer
添加到"performers"
,也可以检索 event_features
。这将把它添加到JSON输出:
'performers': {
u'performer': {
u'name': u'Kim Waters',
u'creator': u'evdb',
u'url': u'http://concerts.eventful.com/Kim-Waters?utm_source=apis&utm_medium=apim&utm_campaign=apic',
u'linker': u'evdb',
u'short_bio': u'Easy Listening / Electronic / Jazz', u'id': u'P0-001-000333271-4'
}
}
要检索图像,请将image
添加到event_features
阵列。但请注意,并非所有事件都有图像。您将看到'image': None
或
'image': {
u'medium': {
u'url': u'http://d1marr3m5x4iac.cloudfront.net/store/skin/no_image/categories/128x128/other.jpg',
u'width': u'128',
u'height': u'128'
},
u'thumb': {
u'url': u'http://d1marr3m5x4iac.cloudfront.net/store/skin/no_image/categories/48x48/other.jpg',
u'width': u'48',
u'height': u'48'
}
}
祝你好运! :)
答案 2 :(得分:0)
如果你查看eventful.com返回的数据,有一些事情是明确的:
这是修改后的代码。希望它能更好地工作,它将帮助你前进。
SRC_URI_common-name = "..."