Answer the question
In order to leave comments, you need to log in
How to get data through the Yandex Schedule API for the current day?
My code:
import json
import requests
from pprint import pprint
link = "https://api.rasp.yandex.net/v3.0/schedule/?apikey=f3a6c277-5bcc-48d9-95ed-6373ba1b6fa5&station=s2014001&transport_types=train&direction=departure&date=2021-01-18"
r = requests.get(link)
trains = json.loads(r.text)
for train in trains["schedule"]:
print(f"Станция: {trains['station']['title']}")
print(f"Отправка в {train['departure']}")
print(f"Номер поезда: {train['thread']['number']}")
print(f"Направление: {train['thread']['title']}\n")
link = "https://api.rasp.yandex.net/v3.0/schedule/?apikey=f3a6c277-5bcc-48d9-95ed-6373ba1b6fa5&station=s2014001&transport_types=train&event=arrival&date=2021-01-18"
r = requests.get(link)
trains = json.loads(r.text)
for train in trains["schedule"]:
print(f"Станция: {trains['station']['title']}")
print(f"Прибытие в: {train['arrival']}")
print(f"Номер поезда: {train['thread']['number']}")
print(f"Направление: {train['thread']['title']}\n")
Answer the question
In order to leave comments, you need to log in
1. What is the problem to get the current data and transfer it?
from datetime import datetime, timedelta
today = datetime.now().strftime('%Y-%m-%d')
from datetime import datetime, timedelta
today_date = datetime.now()
link = "https://api.rasp.yandex.net/v3.0/schedule/?apikey=f3a6c277-5bcc-48d9-95ed-6373ba1b6fa5&station=s2014001&transport_types=train&direction=departure&date=2021-01-18"
r = requests.get(link)
trains = json.loads(r.text)
for train in trains["schedule"]:
date, tz = train['departure'].split('+')
date = datetime.strptime(date, '%Y-%m-%dT%H:%M:%S')
tz = datetime.strptime(tz, '%H:%M')
date = date + timedelta(hours=tz.hour)
if date < today_date:
print('Поезд ушел')
print(f"Станция: {trains['station']['title']}")
print(f"Отправка в {train['departure']}")
print(f"Номер поезда: {train['thread']['number']}")
print(f"Направление: {train['thread']['title']}\n")
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question