我試圖使用事件API來獲取有關兩個日期之間只有音樂事件(音樂會)的信息。例如,我想獲得每場音樂會以下信息從20171012到20171013:從請求到事件API獲取一個空陣列
- 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'}
但是表演者,流派國家和圖像url它不工作。你知道如何獲得這些信息嗎?當我改變下面的python例子來獲取這些信息時,它總是返回一個空數組。
蟒蛇例如工作:(但是,沒有得到表演,流派,國家和圖像的URL,如果我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()
您的'event_features'列表只包含這五個元素。 –
謝謝你的回答。我只是把這5個元素放在一起,因爲就像它的工作一樣。例如,如果我添加執行者,它將返回一個空數組。 – JonD