I've written a script which pulls all of the trains departing and arriving at London Waterloo station and puts the value into a CSV file It works fine locally, but when I run it on pythonAnywhere I'm getting a 403 client error. Any idea why please?
code is below...
thanks!
Rupert
import os, requests, bs4, datetime, csv
trainInfo = ['dep','arr']
timeNow = datetime.datetime.now()
trainFile = open('train.csv','a', newline='')
csvWriter = csv.writer(trainFile)
for trainType in trainInfo:
watPage = requests.get('http://ojp.nationalrail.co.uk/service/ldbboard/%s/WAT' % trainType)
watPage.raise_for_status()
watSoup = bs4.BeautifulSoup(watPage.text, "html.parser")
section1 = watSoup.select('div.results.trains div.tbl-cont table tbody tr')
for table_row in section1:
cells = table_row.findAll('td')
if len(cells) > 0:
ttime = cells[0].text.strip()
tdest = cells[1].text.strip()
tplatform = cells[3].text.strip()
tstatus = cells[2].text.strip()
if tplatform == '':
continue
csvWriter.writerow([timeNow, trainType, ttime, tdest, tplatform, tstatus])
trainFile.close()