📜  pandan jaya lrt - Python (1)

📅  最后修改于: 2023-12-03 15:18:13.502000             🧑  作者: Mango

Pandan Jaya LRT - Python

If you are a Python programmer looking for a way to retrieve information about Pandan Jaya LRT, you are in the right place! In this brief guide, we will show you how to use Python libraries to retrieve information about Pandan Jaya LRT, such as train schedules, arrival times, and more.

Getting Started

First, make sure you have Python installed on your system. You can download the latest version of Python from the official website: https://www.python.org/downloads/

Once you have Python installed, you can start by installing the necessary libraries:

pip install requests
pip install beautifulsoup4

These two libraries will allow us to make HTTP requests to retrieve data from websites, and parse the HTML to extract the information we need.

Retrieving Train Schedules

To retrieve train schedules for Pandan Jaya LRT, we will use the official Rapid KL website. We will make a GET request to the following URL:

https://www.myrapid.com.my/traveling-with-us/train/traintimetable?startStation=43&endStation=22&dayType=WEEKDAY

This URL contains the start and end stations (in this case, Pandan Jaya and KL Sentral) and the day type (weekday).

Here is the Python code to make the request:

import requests
from bs4 import BeautifulSoup

url = 'https://www.myrapid.com.my/traveling-with-us/train/traintimetable?startStation=43&endStation=22&dayType=WEEKDAY'
response = requests.get(url)

soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find_all('table')[0] 
rows = table.find_all('tr')[1:] 

for row in rows:
    cols = row.find_all('td')
    print(cols[0].text, cols[1].text, cols[2].text, cols[3].text)

This code will retrieve the train schedule for Pandan Jaya LRT during weekdays, and print the results to the console.

Conclusion

In this guide, we showed you how to retrieve information about Pandan Jaya LRT using Python libraries. You can use this code as a starting point to build more complex scripts and applications, such as retrieving information for different stations or days of the week.

Remember, always respect the terms of service of the websites you are scraping, and use their data only for non-commercial purposes. Happy programming!