Basically I'm doing a Python script that returns a list of all of the trains' depart time from today, from a certain stop (as you may see on the POST parameters), but it's just returning the last train, for some reason.
Current code:
import requests
from bs4 import BeautifulSoup
import datetime
import calendar
def get_todays_trains():
now = datetime.datetime.now()
url = 'https://www.cp.pt/sites/passageiros/en/train-times/Train-time-results'
r = requests.post(url, allow_redirects=False, data={
'arrival': 'Porto - Campanha',
'depart': 'Aguas Santas - Palmilheira',
'departDate': str(now.year) + '-' + str(now.month) + '-' + str(now.day),
'Date': str(now.day) + ' ' + calendar.month_name[now.month] + ', ' + str(now.year)
})
html = r.text
soup = BeautifulSoup(html, 'html.parser')
for row in soup.findAll('tbody')[1].tbody.findAll('tr'):
depart = row.findAll('td')[2]
print(depart)
print('departDate: ' + str(now.year) + '-' + str(now.month) + '-' + str(now.day))
print('Date: ' + str(now.day) + ' ' + calendar.month_name[now.month] + ', ' + str(now.year))
return depart
get_todays_trains()
If you don't want to go to the page, here's a stripped down version of the HTML from the page: