I'm Swiss and have a problem by creating an app to show trains live where they are. I wanna get information from the website https://rail4.sbb.ch. I can log in me, but then I don't know how to enter text in the "Zugnummer" input element to scrape the "Letzter bekannter Zugstandrort" output. There's my code:
from tkintermapview import TkinterMapView
from tkinter import *
import json
import mechanize
from bs4 import BeautifulSoup
import urllib
train_num = input("Zugnummer: ")
br = mechanize.Browser()
br.open("https://rail4.sbb.ch/Rail4/de")
br.select_form(nr=0)
br.form['USERNAME'] = 'mrt100'
br.form['PASSWORD'] = 'murt0922'
br.submit()
br.select_form(nr=0) #Here's the problem
field = br.form.find_control(id="mat-input-1")
br.form[field] = train_num
br.submit()
res = str(br.response().read())
index = res.find("Letzter Bekannter Zugstandort") + 89
point = res[index:index+6]
point = point.replace(" ", ",")
index = point.index(",")
point = point[:index]
check = True
num = 0
with open("linie-mit-betriebspunkten.json") as json_file: #File from https://data.sbb.ch/explore/dataset/linie-mit-betriebspunkten
data = json.load(json_file)
while check:
if data[num]["fields"]["abkurzung_bpk"] == point:
coordinates = data[num]["fields"]["geopos"]
check = False
num += 1
window = Tk()
window.geometry = ("600x400")
address = str(coordinates[0]) + ", " + str(coordinates[1])
map_widget = TkinterMapView(window, width=600, height=400, corner_radius=0)
map_widget.pack(fill="both", expand=True)
map_widget.set_tile_server("https://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga", max_zoom=20)
map_widget.set_address(address, marker=True)
window.mainloop()