目标

给定指定的站点信息(始发站和终点站)和发车时间,得到相应的信息表格

代码


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 import PrettyTabledef driver_construction():driver_path = r'C:\Users\dell\Anaconda3\Scripts\chromedriver.exe'driver = webdriver.Chrome(driver_path)return driverdef parse_argv(argv_lst):departure_station = argv_lst[1]destination = argv_lst[2]departure_time = argv_lst[3]time_match = re.search('(.*?)-(.*?)-(.*)', departure_time)year = time_match.group(1)month = time_match.group(2)day = time_match.group(3)month_dic = {"01":"一月","02":"二月","03":"三月","04":"四月","05":"五月","06":"六月","07":"七月","08":"八月","09":"九月","10":"十月","11":"十一月","12":"十二月",}month = month_dic[month]day_dict = {"01": "1","02": "2","03": "3","04": "4","05": "5","06": "6","07": "7","08": "8","09": "9",}if day in day_dict:day = day_dict[day]time = (year,month,day)return departure_station,destination, timedef input_information_request_page(driver, departure_station,destination, time):main_url = "https://www.12306.cn/index/"main_page = driver.get(main_url)# 将用户输入的信息传入页面中的文本框处year,month,day = timeinput_departure_box = driver.find_element_by_id("fromStationText").click()departure_station_click = driver.find_element_by_xpath('//li[@title="%s"]'%departure_station).click()input_destination = driver.find_element(By.ID,value="toStationText").click()destination_click = driver.find_element_by_xpath('//li[@title="%s"]'%destination).click()input_departure_time = driver.find_element(By.ID,value="train_date").click()year_click = driver.find_element_by_xpath('//div[@class="year"]//input[@type="text"]').click()click = driver.find_element_by_xpath('//li[text()="%s"]' % year).click()month_click = driver.find_element_by_xpath('//div[@class="month"]//input[@type="text"]').click()click = driver.find_element_by_xpath('//li[text()="%s"]' % month).click()day_click = driver.find_element_by_xpath('//div[text()=%d]' % int(day)).click()# 点击查询按钮search_button = driver.find_element_by_id("search_one").click()# 将driver转到最新的页面上去driver.switch_to_window(driver.window_handles[-1])# 获取最新打开的页面源码page_source = driver.page_sourcereturn page_sourcedef attain_infos_from_page_source(page_source):html = etree.HTML(page_source)diction = {}checi_lst = html.xpath('//a[@title="点击查看停靠站信息"]/text()')print(checi_lst)for checi in checi_lst:info = html.xpath('//tr[contains(@id,"%s")]//text()' % checi)if info:info.remove(info[1])info.remove(info[1])info = list(info)[0:7]info2 = html.xpath('//td[contains(@hbid,"%s")]//text()' % checi)info2 = list(info2)info = info + info2info = list(info)info = list(map(lambda x:x.replace(' ',''),info))info = list(map(lambda x:x.ljust(6),info))# diction[checi] = info[1:]if len(info) == 18:diction[checi] = infocolumns_index = ["车次", "出发站", "到达站", "出发时间", "到达时间", "历时", "到达状态", "商务座", "一等座", "二等座", "高级软卧", "软卧", "动卧", "硬卧","软座", "硬座", "无座", "其他"]columns_index = map(lambda x: x.ljust(6), columns_index)columns_index = tuple(columns_index)diction["title"] = columns_indexreturn dictiondef print_form(diction):tab = PrettyTable()         # 创建格式化表格columns_index = diction["title"]tab.field_names = columns_index     # 把title行作为第一行del diction["title"]                # 把 diction 中 title 这行删除for value in diction.values():tab.add_row(value)return tabif __name__ == '__main__':argv_lst = sys.argvdriver = driver_construction()departure_station, destination, time = parse_argv(argv_lst)print(departure_station, destination, time)page_source = input_information_request_page(driver, departure_station, destination, time)diction = attain_infos_from_page_source(page_source)tab = print_form(diction)print(tab)with open('table.txt','w',encoding='utf-8') as f:f.write(str(tab))

结果

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

  1. 【Python爬虫实战】使用Selenium爬取QQ音乐歌曲及评论信息

    本文对使用到的技术仅做简单的介绍,若想了解更多,请前往相应的官网网站进行学习. 本文适合对爬虫相关知识接触不多的新手,主要是普及Selenium如何做爬虫,大佬请跳过. 1.Selenium简单介绍 ...

  2. Python 爬虫篇-利用BeautifulSoup库爬取墨迹天气网的天气信息实例演示,调用墨迹天气api接口获取空气质量

    安装方法: pip install BeautifulSoup4 BeautifulSoup 详细使用文档 墨迹天气抓取演示 墨迹天气没有提供专门的天气接口 api,但我们可以用 BeautifulS ...

  3. Python新手爬虫训练小项目《爬取彼岸图网》(超详细讲解版)

    Python新手爬虫训练小项目<爬取彼岸图网>(超详细讲解版) 这是我的第一篇文章,作为一名新手爬虫,这个算是我这几天来的努力成果,虽然代码寥寥几行但花费了大半天,新手上路还是不能只看视频 ...

  4. python爬虫之股票数据定向爬取

    python爬虫之股票数据定向爬取 功能描述 目标:获取上交所和深交所所有股票的名称和交易的信息 输出:保存到文件中 技术路线:requests-bs4-re 前期分析 选取原则:股票的信息静态存在H ...

  5. Python 爬虫实战,模拟登陆爬取数据

    Python 爬虫实战,模拟登陆爬取数据 从0记录爬取某网站上的资源连接: 模拟登陆 爬取数据 保存到本地 结果演示: 源网站展示: 爬到的本地文件展示: 环境准备: python环境安装 略 安装r ...

  6. Python爬虫利用18行代码爬取虎牙上百张小姐姐图片

    Python爬虫利用18行代码爬取虎牙上百张小姐姐图片 下面开始上代码 需要用到的库 import request #页面请求 import time #用于时间延迟 import re #正则表达式 ...

  7. python爬虫 - 起点女生榜单爬取 - 1

    python爬虫 - 起点女生榜单爬取 ​ 最近一直在追庆余年,顺带瞄了一眼小说,真真是精彩(虽然因为范闲多妻的设定接受不了就放弃了). ​ 说来说去,还是钟爱女频的修仙小说,所以就想爬一下起点女生网 ...

  8. Python爬虫实战系列(一)-request爬取网站资源

    Python爬虫实战系列(一)-request爬取网站资源 python爬虫实战系列第一期 文章目录 Python爬虫实战系列(一)-request爬取网站资源 前言 一.request库是什么? 二 ...

  9. python项目开发案例集锦 豆瓣-Python第三个项目:爬取豆瓣《哪吒之魔童降世》 短评...

    前面爬完网站信息图片之后,今天的又有了个小目标,最近的电影哪吒很火,去豆瓣上看了一下 影评,决定了今天主要是实现Python第三个项目:爬取豆瓣<哪吒之魔童降世> 短评,然后下载在exce ...

  10. 从入门到入土:Python爬虫学习|实例练手|爬取猫眼榜单|Xpath定位标签爬取|代码

    此博客仅用于记录个人学习进度,学识浅薄,若有错误观点欢迎评论区指出.欢迎各位前来交流.(部分材料来源网络,若有侵权,立即删除) 本人博客所有文章纯属学习之用,不涉及商业利益.不合适引用,自当删除! 若 ...

最新文章

  1. 比“敲低基因”更可怕的是这些项目,看完青少年科技创新大赛完整名单,读研的我自闭了...
  2. getAffineTransform函数
  3. 【设计干货】Facebook设计APP时的14个必考题
  4. java发送内嵌图片邮件
  5. Renascence架构原理——最优化算法
  6. 如何给女朋友解释什么是3PC?
  7. 2008年度一个下岗程序员的真实经历
  8. A站有一个页面需要PV统计 A站读写该数据 B站读该数据 需要数据同步
  9. 爬虫如何监听插件_NodeJS概述2-事件插件-简易爬虫
  10. ApacheCN 人工智能知识树 v1.0
  11. OpenShift 4 - 用 Quay Operator 安装 Quay 环境(4.10 修正)
  12. STM32系列单片机向量表和向量表重新定位
  13. 弹出“FRM-40400:事务完成:已应用和保存X条记录
  14. HTML——表格的快速生成以及表格的合并
  15. H3C S5500V2交换机误格式化恢复
  16. echarts 北京公交线路-线路效应
  17. 【网易互娱内推】2023届秋招提前批内推码:x8aiNS
  18. 下一个五年,存储的生意在哪里?
  19. 飞利浦净化器还能走多远
  20. 【便签1】-win10显示/隐藏任务栏时间

热门文章

  1. python矩阵连乘_python动态规划解决矩阵连乘
  2. matlab提取汉字拼音,中文转拼音工具
  3. 2015.11-12 maxon电机接线——调试——控制
  4. 最简单的正交试验教程
  5. 西藏民族大学计算机学院,西藏民族大学信息工程学院
  6. 支持大规模视频融合的混合现实技术
  7. 归并算法 merge
  8. 使用iTunes 12.7 可以直接安装ipa安装包
  9. 物联网行业系列深度报告 附下载
  10. 电阻屏和电容屏的区别