使用 Python 獲取印度鐵路車站程式碼


網路爬蟲只是靈活的程式語言 Python 的眾多用途之一。在這篇博文中,我們將瞭解如何使用 Python 提取印度鐵路的車站程式碼。每個印度鐵路車站都有一個唯一的識別號,即車站程式碼。它們用於預訂車票、檢視列車時刻表和查詢其他相關資料。

安裝

首先,我們需要安裝 requests 和 Beautiful Soup 庫。Requests 是一個用於傳送 HTTP 請求的 Python 庫,而 Beautiful Soup 是一個用於網路爬蟲的庫。

要安裝 requests,請開啟您的終端並輸入 -

pip install requests
pip install beautifulsoup4

演算法

  • 定義一個名為 get_html 的函式,該函式以 URL 作為輸入。

  • 在函式內部,建立一個包含使用者代理、接受和接受語言值的標頭字典。

  • 使用 requests.get 方法使用標頭字典向 URL 發出 GET 請求,並將響應儲存在名為 response 的變數中。

  • 從函式返回響應文字。

  • 定義一個名為 get_station_code 的函式,該函式以車站名稱作為輸入。

  • 透過將車站名稱連線到基本 URL 來構建車站頁面的 URL。

  • 使用構建的 URL 呼叫 get_html 函式以檢索頁面的 HTML 資料,並將結果儲存在名為 html_data 的變數中。

  • 使用 BeautifulSoup 庫解析 HTML 資料,並將結果儲存在名為 soup 的變數中。

  • 使用 soup 物件的 find 方法查詢具有類 extrtable 的 table 元素,該元素包含車站程式碼。

  • 使用 soup 物件的 find_all 方法查詢 table 元素內的所有 b 元素。

  • 使用 -1 索引檢索 b 元素列表中的最後一個元素,並使用 get_text 方法獲取其文字值。

  • 從函式返回車站程式碼。

  • 使用車站名稱作為輸入呼叫 get_station_code 函式以檢索車站程式碼。

  • 將車站程式碼列印到控制檯。

示例

import requests
from bs4 import BeautifulSoup

# function to get html data from a url
def get_html(url):
   headers = {
      'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) ',
      'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
      'Accept-Language': 'en-US,en;q=0.5',
   }
   response = requests.get(url, headers=headers)
   return response.text

# main function to get station code from mapsofindia.com
def get_station_code(station_name):
   # construct url for the station page
   url = f"https://www.mapsofindia.com/railways/station-code/{station_name}.html"
    
   # get html data for the station page
   html_data = get_html(url)
    
   # parse html data using BeautifulSoup
   soup = BeautifulSoup(html_data, 'html.parser')
    
   # extract station code from html data
   station_code = soup.find("table", class_="extrtable").find_all('b')[-1].get_text()
    
   # return station code
   return station_code

# example usage
station_name = "pune-junction"
station_code = get_station_code(station_name)
print(f"Station Code for {station_name.title()} is {station_code}")

station_name = "new-delhi"
station_code = get_station_code(station_name)
print(f"Station Code for {station_name.title()} is {station_code}")

輸出

Station Code for Pune-Junction is PUNE
Station Code for New-Delhi is NDLS

解釋

此 Python 指令碼用於使用網路爬蟲獲取給定車站名稱的印度鐵路車站程式碼。

指令碼首先匯入必要的模組 - requests 和來自 bs4 的 BeautifulSoup。requests 模組用於傳送 HTTP 請求,而 BeautifulSoup 用於解析 HTML 資料。

然後定義方法 get_html(),它以 URL 作為輸入並返回頁面的 HTML 資訊,該資訊使用 requests 模組向提供的 URL 傳送 HTTP GET 請求,然後將頁面的 HTML 資訊作為文字字串返回。主要方法 get_station_code() 在接收車站名稱作為輸入後返回車站程式碼。車站名稱首先用於建立車站頁面的 URL。然後,使用此 URL 作為引數使用 get_html() 方法獲取頁面的 HTML 資訊。

然後使用 BeautifulSoup 解析 HTML 資料。透過將 HTML 資料和 html.parser 傳遞給 BeautifulSoup 類建構函式來建立 soup 物件。然後,透過搜尋具有類“extrtable”的 <table> 元素來從解析的 HTML 資料中提取車站程式碼。此表包含所有車站資訊,包括車站程式碼,車站程式碼由表中的最後一個 <b> 標記標識。.get_text() 方法用於獲取此標記的文字內容,即車站程式碼。

最後,get_station_code() 函式返回車站程式碼,然後使用 f 字串與車站名稱一起列印。

應用

說明性示例有可能擴充套件到用於各種應用程式中,包括開發使購買車票更簡單、提供列車資訊或允許使用者檢視時刻表的軟體。考慮開發一個預訂火車票的應用程式,使用者可以在其中輸入程式碼以獲取特定車站的車站程式碼,然後再獲取正確的車票。

結論

Python 是一種功能強大的語言,可用於各種用途,包括網路爬蟲。在本博文中,我們學習瞭如何使用 Python 提取印度鐵路車站程式碼。我們分別使用 requests 和 BeautifulSoup 庫傳送 HTTP 請求和解析 HTML 資料。我們還學習瞭如何為特定車站構建 URL 並從 HTML 資料中提取其車站程式碼。此程式碼可用於各種應用程式,包括構建提供列車資訊、預訂車票或檢查列車時刻表的應用程式。

更新於: 2023-07-18

210 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.