Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

How to export IRCTC data to Excel

Last updated on 2 months ago
C
caaSuper Admin
Posted 2 months ago
from bs4 import BeautifulSoup
import requests
import pandas as pd

# Example URL for fetching train schedule from a website
url = 'https://www.trainman.in/train/12056'

# Get the webpage content
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

# Parse station information
stations = []
for row in soup.find_all('div', class_='train-row'):
station = row.find('div', class_='station-name').text.strip()
arrival = row.find('div', class_='arrival-time').text.strip()
departure = row.find('div', class_='departure-time').text.strip()
distance = row.find('div', class_='distance').text.strip()
day = row.find('div', class_='day').text.strip()

stations.append({
'Station Name': station,
'Arrival Time': arrival,
'Departure Time': departure,
'Distance (km)': distance,
'Day': day
})

# Convert to DataFrame
df = pd.DataFrame(stations)

# Save to Excel
file_name = "scraped_train_schedule.xlsx"
df.to_excel(file_name, index=False)

print(f"Train schedule saved to {file_name}")
C
caaSuper Admin
Posted 2 months ago
4. Export Data to Excel:

In the above code, the pandas.DataFrame.to_excel() method is used to export the fetched train schedule to an Excel file.
5. Web Scraping IRCTC Data (Alternative to API):

If you don’t have API access, you can scrape data directly from the IRCTC website or other train information sites (like Trainman, Indian Railways, etc.). However, this method might be legally restricted and less reliable due to changes in the website structure.

Here’s a simple example using BeautifulSoup to scrape train schedule data from a public website:
C
caaSuper Admin
Posted 2 months ago
import requests
import pandas as pd

# Your IRCTC API key (example from railwayapi.com)
api_key = "your_api_key_here"

# Example: Train schedule API (replace with actual API endpoint)
train_number = "12056" # Example train number
url = f"https://api.railwayapi.com/v2/route/train/{train_number}/apikey/{api_key}/"

# Fetch train schedule
response = requests.get(url)
data = response.json()

# Extract useful data (train name, schedule, stations)
train_name = data['train']['name']
schedule_data = []

for station in data['route']:
schedule_data.append({
'Station Name': station['station']['name'],
'Arrival Time': station['scharr'],
'Departure Time': station['schdep'],
'Distance (km)': station['distance'],
'Day': station['day']
})

# Convert to DataFrame
df = pd.DataFrame(schedule_data)

# Save to Excel
file_name = f"{train_name}_schedule.xlsx"
df.to_excel(file_name, index=False)

print(f"Train schedule saved to {file_name}")
Edited by caa on 19-09-2024 22:04, 2 months ago
C
caaSuper Admin
Posted 2 months ago
To export IRCTC data to Excel, you can use Python to scrape or fetch the data from IRCTC using an API and then store it in an Excel file using libraries like pandas or openpyxl.

Here’s a general approach to fetch IRCTC train ticket data and export it to an Excel file:
1. IRCTC API Access:

If you have access to the IRCTC API, you can directly fetch train details, seat availability, booking status, etc. You can sign up for the IRCTC API via official partners like Railway Enquiry (railwayapi.com) or IRCTC's official services.
2. Install Necessary Libraries:

You’ll need the following Python libraries:

requests (to interact with the IRCTC API or website)
pandas (to create and export to Excel)

You can install these libraries by running: pip install requests pandas openpyxl

3. Fetch Data from IRCTC (Using API or Web Scraping):

If you are using an API, the process will be easier. For example, with Railway Enquiry API, you can get train schedules, PNR status, etc. Here is an example to fetch train schedule data.
Edited by caa on 19-09-2024 22:04, 2 months ago
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 2
Members Online 0

Total Members: 11
Newest Member: Jhilam