【本文更新于2020-7-24】
利用Python,通过对列车车次查询网站的内容进行爬虫爬取,获得全国客运列车车次及其起讫站点位置信息(经纬度)。值得注意的是,这些地点的坐标基于高德坐标系,因此在可视化的过程中,推荐使用高德开放平台的“数据可视化”功能。
下面给出参考代码。
注意:
(1)在使用高德开放平台提供的各项功能之前,需申请密钥(key)。这一加密字符串的功能,类似百度平台的“ak”。
(2)在使用Python爬虫前,需安装requests、bs4等库。
(3)高德开放平台提供的各项功能的使用方法,可参考官方提供的使用说明。
#获取铁路列车信息
import xlwt
import requests as rq
from bs4 import BeautifulSoup as bs
import time
from retry import retry
rootpath = 'E:/PythonChris/Train_Schedule/'
#所有火车车次网站
site_of_train = 'http://www.jt2345.com/huoche/checi'
@retry(tries=2, delay=2)
def geturl(url):
headers = {
'User-Agent':'Chrome/72.0'}
r = rq.get(url,headers = headers,timeout = 5)
r.raise_for_status
r.encoding = 'GB2312'
txt = r.text
return txt
#收集所有车次信息
@retry(tries=2, delay=2)
def gettrains(txt):
trains = []
soup = bs(txt,'html.parser')
for l in soup.find_all