python 制作火车时刻表

利用flask构造爬虫接口(python初级) 爬虫很多时候除了能够做成项目在服务器上跑以外,还可以做成接口的形式够其他语言调用,即爬虫可以实时的镶嵌到app,web或者其他场景下,当有客户调用时就会启动,无人调用时,就静静的待着。下面说一下利用python中的flask框架来写一个简单的爬虫来供其他语言调用。初级入门很简单的web框架和爬虫之间的结合应用。如果有其他的应用需求,请自行研究。 首先,导包不解释了,自行安装flask就可以了。 ... 阅读详情

python 制作火车时刻表

火车时刻表是一种方便快捷的查询火车班次信息的工具,那么,如何实现用python实现爬取这些网站的时刻表数据呢?

一、访问 http://www.ip138.com/ 查询网的火车时刻表工具:http://qq.ip138.com/train/

网页截图

选择“列车车次查询”,并使用CHROME开发者工具,可以发现并没有有关数据的XHR包,这是我们选择用正则表达式提取数据。

选择车次,可发现网址格式为:

http://qq.ip138.com/train/<车次号>.htm

标题正则

<h1>.*</h1>

火车类型数据正则:

列车类型:.*&nbsp;

车站名正则
 

<a href="/train.*" target="_blank">.*</a>

时间数据正则(记得不要第一个):

([0-9]{2}:[0-9]{2})|(---)

通过python的RE模块,可以很方便的查询正则数据。

附上源代码(prettytable模块用于显示表格数据,colorama用于设置文本颜色)

import requests
# from urllib import parse
import prettytable
import re
import colorama
def real(l):
    for i in l:
        if(i != ''):
            return i
    return -1

code=input("输入班次:\n")
url="http://qq.ip138.com/train/%s.htm"%code

data=requests.get(url)
encode=data.encoding
# print(encode)
if(data.status_code == 404):
    print("没有查到:%s"%code)
    exit(1)
data=data.content.decode("gb2312")

title=re.search("<h1>.*</h1>",data).group(0)
title=title.replace("<h1>","")
title=title.replace("</h1>","")
start=re.search("从.*开往",title).group(0).replace("从","")\
    .replace("开往","")
end=re.search("往.*%s"%code.lower(),title).group(0).replace("往","").replace(code.lower(),"")
train_type=re.search("列车类型:.*&nbsp;",data).group(0).replace("&nbsp;","").replace("</td>","").replace("<td>","")
print(title)
print()
print(colorama.Style.BRIGHT+"从%s开往%s"%(start,end)+
      colorama.Style.RESET_ALL)
print(train_type)
train_station=re.findall("<a href=\"/train.*\" target=\"_blank\">.*</a>",data)
# print("-"*100)
train_time=re.findall("([0-9]{2}:[0-9]{2})|(---)",data)
train_time.pop(0)
# print(train_time)
print("")
tableHead=["车站","到达时间","发车时间","走行时间(小时)"]
for i in tableHead:
    tableHead[tableHead.index(i)]=colorama.Fore.GREEN+i+colorama.Fore.RESET
tableObj=prettytable.PrettyTable(tableHead)
tableObj.header=True
tableObj.horizontal_char = '-'
tableObj.junction_char='|'
cnt=0
for i in train_station:
    row=[]
    j=i.replace("</a>","")

    e=j.index(">")

    # print(j[e+1:],
    #       real(train_time[cnt]),
    #       real(train_time[cnt+1]),
    #       real(train_time[cnt+2]))
    row.append(colorama.Fore.RED+j[e+1:]+colorama.Fore.RESET)
    # print(colorama.Back.BLACK)
    row.append(colorama.Fore.LIGHTMAGENTA_EX+real(train_time[cnt]))
    row.append(real(train_time[cnt+1]))
    row.append(real(train_time[cnt+2]))
    print(colorama.Fore.RESET)
    tableObj.add_row(row)
    cnt=cnt+3
# print(colorama.Back.BLACK)
print(tableObj)
# print(colorama.Back.RESET)

real模块用于避免findall函数显示不对

仓库:https://gitee.com/pybilly/train_timetable/tree/master/

 

爬虫小白入门实例 —— 爬取全国某天所有火车的运行时刻表 受好朋友的委托,帮忙爬取全国某天所有火车的运行时刻表。 在此之前没有用过爬虫,但是会用python,所以迅速学习了一下。 把自己的学习过程整理如下,爬虫小白可以通过下述内容快速入门。 任务描述: 爬取的站点:http://search.huochepiao.com/chaxun/resultc.asp?txtCheci=D2&amp;cc.x=0&amp;cc.y=0 希望能把当前运行的... 阅读详情

相关推荐

使用Python爬虫抓取公共交通网站的时刻表

Python爬虫是一种通过自动化方式从网页中抓取数据的程序。爬虫通过模拟用户请求,获取网站的HTML页面内容,并通过解析这些HTML内容,提取出我们需要的信息。requests:用于向网站发送HTTP请求并获取响应。:用于解析HTML文档,提取数据。Selenium:用于处理动态加载的网页,模拟浏览器操作。pandas:用于数据处理和分析,方便存储和操作抓取到的数据。本文介绍了如何使用Python爬虫抓取公共交通网站的时刻表,并提取其中的关键信息。我们使用了requestsSelenium和。

2201_76125261的博客 1125

爬虫--列车时刻表数据(python)

这样打印出来,就是序号那一列,其余列也是同样的方式解析,具体每一个元素他在什么路径下,可以去源代码页面复制某个具体数据的 XPATH,然后把前面和表格 XPATH 一样的部分用一个点。请求网页的过程其实就是让软件模拟我们点击网页中按钮的过程,这里需要讲到一些html的知识,不再拓展,关键的是防止被网站的反爬程序封ip的方法就是设置一个请求头headers。到这里我们就请求到了网页,这里可以理解为得到了整个网页的源代码,下面我们要在整个网页的源代码里找到我们所需要的数据,也就是下一步,解析网页数据。

m0_61165991的博客 2333

全国最新列车时刻表解析与应用

时刻表是一份详细记录列车运行时间、停靠站点和行程距离的官方文档。它对乘客来说至关重要,因为它提供了规划旅行所需的全部信息,包括发车和到达的具体时间。对运营商而言,时刻表是确保列车准时、高效运营的基础。

weixin_34640289的博客 1130

Python列车信息爬虫

Python列车信息爬虫 Python爬虫我已经写过很多了,其实很大部分都是在重复。所以这篇或者是以后的博客都不会从头到尾讲爬虫,只是会将之前没有遇到的问题给重点列出来。 1.任务分析 这是我们要爬取的网站,我们要做的就是输入出发地和目的地 然后点击提交按钮。例如输入上海,北京,点击提交即会出现这个如下页面。  这是有直达的情况,当然也有非直达的情况,例如大连和东莞。  所以...

123的博客 601

画出列车运行图,给出列车运行的最佳调度(python代码)

画出列车运行图,给出列车运行的最佳调度(python代码)

深入解析SDC时钟约束:从create_clock到set_clock_groups的实战指南

本文深入解析SDC时钟约束,从create_clock到set_clock_groups的实战应用,帮助数字芯片设计工程师掌握时序约束的核心技巧。通过详细讲解时钟三要素(波形定义、不确定性和时钟关系)及实战案例,提升时钟约束的精准性和效率,避免常见设计陷阱。

weixin_29274337的博客 1089

Python爬虫自己写项目之:爬取火车站的时刻表和票务信息

目标 给定指定的站点信息(始发站和终点站)和发车时间,得到相应的信息表格 代码 from selenium import webdriver import sys from selenium.webdriver.common.by import By from selenium.webdriver.support.select import Select import re from lxml import etree import pandas as pd from prettytable impo

qq_42902997的博客 1800

技术前沿:半导体前道检测和量测设备修复技术

半导体前道检测设备是目前除光刻机外,国产渗透率较低的环节之一,近年来我国初步实现国产突破。前道检测相对后道检测环节壁垒更高,国内多家涉及后道测试,涉及前道检测和中道检测相对较少。

VOMMA的博客 4555

find_all可以连续使用_使用python 制作火车查询时刻表!

火车时刻表是一种方便快捷的查询火车班次信息的工具,那么,如何实现用python实现爬取这些网站的时刻表数据呢?一、访问 http://www.ip138.com/ 查询网的火车时刻表工具:http://qq.ip138.com/train/选择“列车车次查询”,并使用CHROME开发者工具,可以发现并没有有关数据的XHR包,这是我们选择用正则表达式提取数据。选择车次,可发现网址格式为:http:/...

weixin_36083698的博客 356

从X86到ARM:全面解析服务器CPU架构的演变与未来趋势

本文深入解析了服务器CPU架构从X86到ARM的演变历程与未来趋势。文章从CISC与RISC两种设计哲学切入,分析了X86凭借生态建立的帝国地位及其面临的功耗挑战,并重点阐述了ARM架构凭借其卓越的能效比优势,正从移动端向数据中心市场发起冲击。未来,异构计算、软硬协同与专用芯片将成为服务器CPU发展的核心方向。

gold的博客 1132

K8S搭建(centos)完整版

每个人都有惰性,但不断学习是好好生活的根本,共勉!

感谢阅读 4153

python爬取火车票网的时刻表数据

python爬取火车票网的时刻表数据 导包 ** import re,requests,datetime,time,json from prettytable import PrettyTable from colorama import init,Fore from pyquery import PyQuery as pq import random import pymysql.cursors ** 导包遇到问题的解决方法 1.在pycharm中: File ——> Setting——>Pr

Inmaturity_7的博客 1278

国科大学习资料--高级软件工程-复习题选择题答案

国科大学习资料–高级软件工程(罗铁坚 魏峻 刘丹)-复习题选择题答案 ’‘

无名何许人的博客 1170

Python爬虫爬取铁路列车时刻表数据

Python爬虫爬取网站列车时刻表数据

Made-In-China-0711的博客 7724

项目实战第四十七讲:易宝支付对接详解(保姆级教程)

本文是项目实战第四十七讲,易宝支付对接详解。1、xxl_job 接入fix;2、支付接口,将thirdOrderNo赋初始值,并将数据库中third_Order_No设置为非空;3、性能提效:将深拷贝改为字符赋值,提升程序性能;先不改动。

Jet_qi的博客 2690

基于YOLOv11的工业装配完整性智能检测系统

在现代工业生产中,产品质量控制是确保企业竞争力的关键环节。其中,装配完整性检查——即检测产品是否安装了所有应有的部件,是质量检测中的重要一环。传统的人工检测方法存在效率低、易疲劳、主观性强等问题。本文提出了一种基于深度学习的目标检测算法YOLOv11的工业装配完整性智能检测系统,通过计算机视觉技术自动识别和验证产品装配的完整性。本文将详细阐述从数据准备、模型训练到系统部署的完整流程,提供可执行的代码实现,并介绍一个适用于该任务的参考数据集。

YOLO 555

python计算火车的运行时间表_python网页爬虫之列车时刻表的抓取(4)-完整的python脚本...

#! /usr/bin/env python#coding=utf8# by meichenhui@gmail.com 2010/5/30from HTMLParser import HTMLParserfrom pyquery import PyQuery as pqimport sqlite3,urllib2,logging,sysfrom datetime import datetimefr...

weixin_29369347的博客 751

二十一、SAP-库存管理

业务逻辑、功能讲解、图文解析、章节总结

m0_72403327的博客 1827

python火车站管理系统制作

python火车站管理系统制作

恰到好处a的博客 470

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

Python 火车停靠站点时间查询

''' Python 火车停靠站点时间查询 by 郑瑞国 trainSearch.py Python 3.7 使用第三方模块 requests ''' import requests from xml.etree import ElementTree while(True): Train_Code = input('请输入车次:') url = 'http://www...

zheng_ruiguo的专栏 1461

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

基于Java+SSM+Django列车火车高铁票务信息管理系统(源码+LW+调试文档+讲解等)/列车时刻表/火车票预订/高铁线路查询/票务查询系统/火车票价查询/高铁票务信息/列车晚点查询

前端:Django、Python Web框架,后端语言Python后端:Spring+SpringMVC+Mybatis数据库:MySQL、SQLServer开发工具:IDEA、Eclipse、Navicat等✌关于毕设项目技术实现问题讲解也可以给我留言咨询!!!在程序设计的安全性方面,Django 提供了多种保障措施。它自动处理 SQL 注入、跨站脚本攻击等常见的安全问题,确保程序的安全性。程序设计者可以专注于业务逻辑的实现,而无需过多担心安全漏洞,降低了开发过程中的安全风险。

2401_85513329的博客 822

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

python36 +tkinter制作12306火车票查询系统

12306是个令人头疼的网站,用python写了一个查询车次的爬虫,余票信息暂时无法正确提取出来12306车次 G  T  车次的余票信息规则诡异,比如1461车次    0|0|后面的四个|分割 对应的是商务座-高级软卧,有 后面又是2个分隔符对应的是哪个座位|20180713|3|P2|01|27|0|0||||有|||有||有|有|||||10403010|1431|0",G129  又比如...

thindi的博客 1859

基于python的火车票查询

使用python进行火车票查询

2301_81526852的博客 467

python网页爬虫之列车时刻表的抓取(4)-完整的python脚本

python网页爬虫之列车时刻表的抓取(4)-完整的python脚本 2010-05-30 18:28 #! /usr/bin/env python #coding=utf8 # by meichenhui@gmail.com 2010/5/30 from HTMLParser import HTMLParser from pyquery import PyQuery as pq

rongwenbin的专栏 2019

Python查询火车票

import re, json, time from selenium import webdriver def get_ticket(): from_city = input("出发站:") to_city = input("到达站:") date = input("出发日期(格式:2019-12-30):") file_name = input("存储文件:")...

qq_39581763的博客 414
上一篇: 关于新的pi公式
下一篇: python 制作CPU压力测试——圆周率计算
谢哲源
博客等级 码龄8年 66粉丝 11原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谢哲源

您的鼓励是我们前进的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址