Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Get Indian Railways Station code Using Python

  • Last Updated : 29 Dec, 2020

Web scraping is a technique to fetch data from websites. While surfing on the web, many websites don’t allow the user to save data for personal use. One way is to manually copy-paste the data, which both tedious and time-consuming. Web Scraping is the automation of the data extraction process from websites. This event is done with the help of web scraping software known as web scrapers.

In this article, we are going to write Python scripts to scrape the Railways Station code using their city name.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


Input: new-delhi
Output: NDLS

Input: Patna
Output: PNBE

Module needed

  • bs4: 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


# import module
import requests
from bs4 import BeautifulSoup

Step 2: Create a URL get function


# user define function 
# Scrape the data 
def getdata(url): 
    r = requests.get(url) 
    return r.text

Step 3:Now merge the City name into URL and pass the URL into the getdata() function and Convert that data into HTML code.


# input by geek
station = "new-delhi"
# url
# pass the url
# into getdata function
soup = BeautifulSoup(htmldata, 'html.parser')
# display html code


Step 4: Traverse the Station code from the HTML document.


# traverse the station code
data = []
for item in soup.find("table", class_="extrtable").find_all('b'):



My Personal Notes arrow_drop_up
Recommended Articles
Page :