Get live train status using Python Last Updated : 17 May, 2022 Comments Improve Suggest changes Like Article Like Report Suppose you want to travel to places using Indian Railways and have booked a train. But you are not sure that the train is on time or not and doing this manually can be very hectic. So in this article, we are going to write a Python script to get live train status using a train name or train code. Modules neededbs4: Beautiful Soup(bs4) is a Python library for pulling data out of HTML and XML files. This module does not come built-in with Python. To install this type the below command in the terminal.pip install bs4 requests: Request allows you to send HTTP/1.1 requests extremely easily. This module also does not come built-in with Python. To install this type the below command in the terminal.pip install requests Let’s see the stepwise execution of the script. Step 1: Import all dependence Python3 # import module # import pandas as pd import requests from bs4 import BeautifulSoup Step 2: Create a URL get function Python3 # user define function # Scrape the data def getdata(url): r = requests.get(url) return r.text Step 3: Now merge the train name into URL and pass the URL into the getdata() function and Convert that data into HTML code. Note: Strongly recommended you to get Train name and code from here. Click Python3 # input by geek train_name = "03391-rajgir-new-delhi-clone-special-rgd-to-ndls" # url url = "https://www.railyatri.in/live-train-status/"+train_name # pass the url # into getdata function htmldata=getdata(url) soup = BeautifulSoup(htmldata, 'html.parser') # display html code print(soup) Output: Step 4: Traverse the live status from the HTML document. Python3 # traverse the live status from # this Html code data = [] for item in soup.find_all('script', type="application/ld+json"): data.append(item.get_text()) # convert into dataframe df = pd.read_json (data[2]) # display this column of # dataframe print(df["mainEntity"][0]) Output: {'@type': 'Question', 'name': 'Q) Where is my train (03391) RGD NDLS HUMSFR ?', 'acceptedAnswer': {'@type': 'Answer', 'text': 'A: 03391 RGD NDLS HUMSFR is 10 kms to VARANASI JN (312 kms Covered so far). It is expected to reach New Delhi by 02:30.'}} Step 5: Now get the required data from this directory. Python3 print(df["mainEntity"][0]['name']) print(df["mainEntity"][0]['acceptedAnswer']['text']) Output: Q) Where is my train (03391) RGD NDLS HUMSFR ? A: 03391 RGD NDLS HUMSFR is 10 kms to VARANASI JN (312 kms Covered so far). It is expected to reach New Delhi by 02:30. Full implementation: Python3 # import module import requests from bs4 import BeautifulSoup import pandas as pd # user define function # Scrape the data def getdata(url): r = requests.get(url) return r.text # input by geek train_name = "03391-rajgir-new-delhi-clone-special-rgd-to-ndls" # url url = "https://www.railyatri.in/live-train-status/"+train_name # pass the url # into getdata function htmldata = getdata(url) soup = BeautifulSoup(htmldata, 'html.parser') # traverse the live status from # this Html code data = [] for item in soup.find_all('script', type="application/ld+json"): data.append(item.get_text()) # convert into dataframe df = pd.read_json(data[2]) # display this column of # dataframe print(df["mainEntity"][0]['name']) print(df["mainEntity"][0]['acceptedAnswer']['text']) Output: Q) Where is my train (03391) RGD NDLS HUMSFR ? A: 03391 RGD NDLS HUMSFR is 6 kms to VARANASI JN (316 kms Covered so far). It is expected to reach New Delhi by 02:30. Comment More infoAdvertise with us Next Article Get live train status using Python kumar_satyam Follow Improve Article Tags : Python python-utility Python-projects Web-scraping Python web-scraping-exercises +1 More Practice Tags : python Similar Reads Building CLI to check status of URL using Python In this article, we will build a CLI(command-line interface) program to verify the status of a URL using Python. The python CLI takes one or more URLs as arguments and checks whether the URL is accessible (or)not. Stepwise ImplementationStep 1: Setting up files and Installing requirements First, cr 4 min read Python - API.get_status() in Tweepy Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from 2 min read Create API Tester using Python Requests Module Prerequisites: Python Requests module, API In this article, we will discuss the work process of the Python Requests module by creating an API tester. API stands for Application Programming Interface (main participant of all the interactivity). It is like a messenger that takes our requests to a syst 3 min read PyQtGraph â Getting Status Tip of Plot Window In this article we will see how we can get status tip to the plot window in the PyQtGraph module. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive grap 3 min read Python Tweepy â Getting the ID of a status In this article we will see how we can get the ID of a status. Each status or tweet has a unique ID. The id attribute of the Status object provides us with the ID of the status/tweet. Identifying the ID in the GUI : In the above mentioned status, the ID can be found in the URL, the ID here is : 1271 2 min read Tweet Sentiment Analysis Using Python Streamlit This article covers the sentiment analysis of by parsing the tweets fetched from Twitter using the streamlit Python framework. What is Sentiment Analysis? Sentiment Analysis is the process of âcomputationallyâ determining whether a piece of writing is positive, negative or neutral. Itâs also known a 4 min read Python | API.update_status() in Tweepy Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from 2 min read Python - API.update_status() in Tweepy Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from 2 min read How to get current_url using Selenium in Python? While doing work with selenium many URL get opened and redirected in order to keeping track of URL current_url method is used. The current_url method is used to retrieve the URL of the webpage the user is currently accessing. It gives the URL of the current webpage loaded by the driver in selenium. 2 min read Querying Live running status and PNR of trains using Railway API in Python Railway API is organized around GET Requests. One can use this JSON based API to get information from Indian Railways regarding Live Train Status, PNR Status, Train Schedule, Station Details, and other things. To use this API, one must need the API key, which can get from here Note: User need to cre 5 min read Like