📜  使用Python获取实时列车状态

📅  最后修改于: 2022-05-13 01:55:00.804000             🧑  作者: Mango

使用Python获取实时列车状态

假设您想使用印度铁路旅行到一些地方并预订了火车。但是您不确定火车是否准时,手动执行此操作可能会非常忙碌。因此,在本文中,我们将编写一个Python脚本来使用列车名称或列车代码获取实时列车状态。

需要的模块

  • bs4 : Beautiful Soup(bs4) 是一个Python库,用于从 HTML 和 XML 文件中提取数据。这个模块没有内置于Python中。要安装此类型,请在终端中输入以下命令。
pip install bs4
  • requests Request 允许您非常轻松地发送 HTTP/1.1 请求。这个模块也没有内置于Python中。要安装此类型,请在终端中输入以下命令。
pip install requests

让我们看看脚本的逐步执行。

第一步:导入所有依赖

Python3
# import module
# import pandas as pd
import requests
from bs4 import BeautifulSoup


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


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)


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])


Python3
print(df["mainEntity"][0]['name'])
print(df["mainEntity"][0]['acceptedAnswer']['text'])


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'])


第 2 步:创建 URL 获取函数

蟒蛇3

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

第 3 步:现在将列车名称合并到 URL 并将 URL 传递到 getdata()函数并将该数据转换为 HTML 代码。

注意:强烈建议您从此处获取列车名称和代码。点击

蟒蛇3

# 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)

输出:

第 4 步:从 HTML 文档中遍历实时状态。

蟒蛇3

# 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])

输出:

第 5 步:现在从该目录中获取所需的数据。

蟒蛇3

print(df["mainEntity"][0]['name'])
print(df["mainEntity"][0]['acceptedAnswer']['text'])

输出:

全面实施:

蟒蛇3

# 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'])

输出: