往期目录

一、

python爬虫入门篇https://blog.csdn.net/weixin_64050468/article/details/130501830?spm=1001.2014.3001.5501

二 、

python爬虫入门篇https://blog.csdn.net/weixin_64050468/article/details/130583771?spm=1001.2014.3001.5501


文章目录

前言

一、按照selenuim模块+配置+可能报错解释

二、selenium部分代码解读

三、开始操作

抢票代码

爬取数据代码


前言

后面文章主打用爬虫整活了 边学边玩


一、安装selenuim模块+配置+可能报错解释

win + r 打开cmd

然后输入 pip install selenuim  下载selenuim

如果显示没有pip 那么你的环境没配好  可以看看这个问题解决 虽然比较老但是操作是一样的

先确定谷歌版本 我的就是113.0.5672.127 64位版本的 但是只有32位能下载也能用

然后下载谷歌驱动 chomedriver 下载

 随便哪个都可以 我的是window系统的就选这个了

 然后解压到你的解释器下面

解释器在哪这种问题 你pip 的时候就会显示出来你的这个下载的地址就是你解释器大概的位置找一下就有了

-

然后试试这段代码 应该就能看到这么个页面了

import time
from selenium import webdriverdriver = webdriver.Chrome() # 启动浏览器
driver.get("https://www.baidu.com") # 打开某个网址
time.sleep(10)
driver.quit()   # 关闭浏览器

二、selenium部分代码解读

selenium使用方法大全

本文只需要使用到
定位
点击
输入
清空

定位:

wb = webdriver.Chrome()
wb.implicitly_wait(30)
wb.get('https://laicj.cn/#/') #网址
wb.find_element(By.XPATH,'/html/body/div/section/main/div/div[1]/div[1]/div[1]/textarea')

wb.implicitly_wait(30) :个人理解为每次定位点击等操作可以有30秒以内的误差时间 比如网页跳转需要时间 在这段时间内没有定位到元素会显示报错
find_element:查找元素的意思
By.XPATH:就是按照xpath定位网页中的元素
元素:可以在控制台找到右键有一个copy xpath/ full xpath 如下图

点击:

wb.find_element(By.XPATH,'/html/body/div/section/main/div/div[1]/div[1]/button').click()

在定位元素后面直接加.click()

输入:

wb.find_element(By.XPATH,'/html/body/div/section/main/div/div[1]/div[1]/div[1]/textarea').send_keys(f'{data_msg}')

在定位元素后面直接加.send_key(‘内容’)

清空:

wb.find_element(By.XPATH,'/html/body/div/section/main/div/div[1]/div[1]/div[1]/textarea').clear()

在定位元素后面直接加.clear()

最后我在源代码中加入了判断语句 为的是判断ai是否写完 如果写完则返回内容

    def yes_or_no():a = wb.find_element(By.XPATH,'/html/body/div/section/main/div/div[1]/div[2]/div/p')time.sleep(10)b = wb.find_element(By.XPATH,'/html/body/div/section/main/div/div[1]/div[2]/div/p')if a == b:return a.text

三、开始操作

代码实现:

def date_n(n):#获取时间函数return str((date.today() + timedelta(days=int(n))).strftime('%Y-%m-%d'))
from_station, to_station = '佛山西', '广州北'
tomorrow = date_n(1)
print(tomorrow)
driver = webdriver.Chrome()
driver.maximize_window() #窗口最大
driver.implicitly_wait(5) #允许有五秒的寻找时间
driver.get('https://www.12306.cn/index/')
driver.find_element(By.XPATH, '//*[@id="fromStationText"]').click()
driver.find_element(By.XPATH, '//input[@id="fromStationText"]').send_keys(from_station)
driver.find_element(By.XPATH, '//*[@id="citem_0"]').click()
driver.find_element(By.XPATH, '//*[@id="toStationText"]').send_keys(to_station)
driver.find_element(By.XPATH, '//*[@id="citem_0"]').click()
driver.find_element(By.XPATH, '//*[@id="train_date"]').clear()
driver.find_element(By.XPATH, '//*[@id="train_date"]').send_keys(tomorrow)
driver.find_element(By.XPATH, '//a[@id="search_one"]').click()
driver.switch_to.window(driver.window_handles[-1])  #点击完会打开新的页面 然后要跳转到新的页面去开始我们下一步的操作 没有的话你会卡在第一个窗口 后续代码运行不了

driver.switch_to.window(driver.window_handles[-1])  #点击完会打开新的页面 然后要跳转到新的页面去开始我们下一步的操作 没有的话你会卡在第一个窗口 后续代码运行不了

后续就是看你们要抢哪一个票 或者爬取数据也可以 如果真的想做好 我觉得应该把数据爬下来做成表然后给用户选择输入最好 当然我这只是做一个简单的讲解就不想做这么深了

当然结尾提供了两个代码 可以试试去整合起来做成一个exe

以前做个一个带ui的 但是由于网页的更新 应该是不能运行了 下次如果有用pyside2的话再说吧


抢票代码

import time
from datetime import date, timedelta
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from pprint import pprint
from tabulate import tabulatedef date_n(n):#获取时间函数return str((date.today() + timedelta(days=int(n))).strftime('%Y-%m-%d'))
from_station, to_station = '佛山西', '广州北'
tomorrow = date_n(1)
print(tomorrow)
driver = webdriver.Chrome()
driver.maximize_window() #窗口最大
driver.implicitly_wait(5) #允许有五秒的寻找时间
driver.get('https://www.12306.cn/index/')
driver.find_element(By.XPATH, '//*[@id="fromStationText"]').click()
driver.find_element(By.XPATH, '//input[@id="fromStationText"]').send_keys(from_station)
driver.find_element(By.XPATH, '//*[@id="citem_0"]').click()
driver.find_element(By.XPATH, '//*[@id="toStationText"]').send_keys(to_station)
driver.find_element(By.XPATH, '//*[@id="citem_0"]').click()
driver.find_element(By.XPATH, '//*[@id="train_date"]').clear()
driver.find_element(By.XPATH, '//*[@id="train_date"]').send_keys(tomorrow)
driver.find_element(By.XPATH, '//a[@id="search_one"]').click()
driver.switch_to.window(driver.window_handles[-1])  #点击完会打开新的页面 然后要跳转到新的页面去开始我们下一步的操作 没有的话你会卡在第一个窗口 后续代码运行不了
before = time.time()driver.find_element(By.XPATH,'/html/body/div[2]/div[8]/div[9]/table/tbody/tr[1]/td[13]/a').click()
sleep(0.5)
driver.find_element(By.XPATH,'//*[@id="J-userName"]').send_keys('账号')
driver.find_element(By.XPATH,'//*[@id="J-password"]').send_keys('密码')
driver.find_element(By.XPATH,'//*[@id="J-login"]').click()
sleep(0.5)
driver.find_element(By.XPATH,'//*[@id="normalPassenger_0"]').click()
driver.find_element(By.XPATH,'/html/body/div[4]/div[2]/div[2]/div[2]/a[2]').click()
driver.find_element(By.XPATH,'//*[@id="submitOrder_id"]').click()  # 确认购买
sleep(0.5)
driver.find_element(By.XPATH,'/html/body/div[6]/div/div[5]/div[1]/div/div[2]/div[2]/div[9]/a[2]').click()#确认购买

爬取数据代码

import time
from datetime import date, timedelta
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from pprint import pprint
from tabulate import tabulatedef date_n(n):#获取时间函数return str((date.today() + timedelta(days=int(n))).strftime('%Y-%m-%d'))
from_station, to_station = '佛山西', '广州北'
tomorrow = date_n(1)
print(tomorrow)
driver = webdriver.Chrome()
driver.maximize_window() #窗口最大
driver.implicitly_wait(5) #允许有五秒的寻找时间
driver.get('https://www.12306.cn/index/')
driver.find_element(By.XPATH, '//*[@id="fromStationText"]').click()
driver.find_element(By.XPATH, '//input[@id="fromStationText"]').send_keys(from_station)
driver.find_element(By.XPATH, '//*[@id="citem_0"]').click()
driver.find_element(By.XPATH, '//*[@id="toStationText"]').send_keys(to_station)
driver.find_element(By.XPATH, '//*[@id="citem_0"]').click()
driver.find_element(By.XPATH, '//*[@id="train_date"]').clear()
driver.find_element(By.XPATH, '//*[@id="train_date"]').send_keys(tomorrow)
driver.find_element(By.XPATH, '//a[@id="search_one"]').click()
driver.switch_to.window(driver.window_handles[-1])  #点击完会打开新的页面 然后要跳转到新的页面去开始我们下一步的操作 没有的话你会卡在第一个窗口 后续代码运行不了
index = []
mid = []
for i in range(1,17):mid.append(driver.find_element(By.XPATH,f'//*[@id="float"]/th[{i}]').text)
index.append(mid.copy())
mid.clear()
for i in range(1,1000):data = []try:for p in range(5):k = driver.find_element(By.XPATH,f'/html/body/div[2]/div[8]/div[9]/table/tbody/tr[{i}]').textif k == '':passelse:data.append(k)index.append(data)except:break
pprint(index)
print(tabulate(index,headers='firstrow', tablefmt='grid'))

带ui的代码和ui

from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import QApplication,QMainWindow,QPushButton,QPlainTextEdit,QMessageBox
import smtplib
import time
import time
from datetime import date, timedelta
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleepclass email:def __init__(self):self.ui = QUiLoader().load('12306.ui')self.ui.button.clicked.connect(self.start)def start(self):name = self.ui.lineEdit.text()time = self.ui.dateTimeEdit.text()cookie = self.ui.lineEdit_4.text()zh_12306 = self.ui.lineEdit_3.text()mm_12306 = self.ui.lineEdit_2.text()now = time.time()def date_n(n):return str((date.today() + timedelta(days=int(n))).strftime('%Y-%m-%d'))from_station, to_station = '佛山西', '广州北'tomorrow = date_n(1)print(tomorrow)driver = webdriver.Chrome()driver.maximize_window()driver.get('https://www.12306.cn/index/')driver.find_element(By.XPATH, '//*[@id="fromStationText"]').click()driver.find_element(By.XPATH, '//input[@id="fromStationText"]').send_keys(from_station)driver.find_element(By.XPATH, '//*[@id="citem_0"]').click()driver.find_element(By.XPATH, '//*[@id="toStationText"]').send_keys(to_station)driver.find_element(By.XPATH, '//*[@id="citem_0"]').click()driver.find_element(By.XPATH, '//*[@id="train_date"]').clear()driver.find_element(By.XPATH, '//*[@id="train_date"]').send_keys(tomorrow)driver.find_element(By.XPATH, '//a[@id="search_one"]').click()driver.switch_to.window(driver.window_handles[1])driver.find_element(By.XPATH,'//*[@id="qd_closeDefaultWarningWindowDialog_id"]').click()driver.find_element(By.XPATH,'//*[@id="ticket_66000K924704_06_07"]/td[13]/a').click()sleep(0.5)driver.find_element(By.XPATH,'//*[@id="J-userName"]').send_keys('账号')driver.find_element(By.XPATH,'//*[@id="J-password"]').send_keys('密码')driver.find_element(By.XPATH,'//*[@id="J-login"]').click()sleep(0.5)driver.find_element(By.XPATH,'//*[@id="normalPassenger_0"]').click()driver.find_element(By.XPATH,'/html/body/div[4]/div[2]/div[2]/div[2]/a[2]').click()driver.find_element(By.XPATH,'//*[@id="submitOrder_id"]').click()  # 确认购买sleep(0.5)# driver.find_element(By.XPATH,'/html/body/div[6]/div/div[5]/div[1]/div/div[2]/div[2]/div[8]/a[2]').click()#确认购买before = time.time()print(now - before)

ui代码

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0"><class>MainWindow</class><widget class="QMainWindow" name="MainWindow"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>600</height></rect></property><property name="windowTitle"><string>MainWindow</string></property><widget class="QWidget" name="centralwidget"><widget class="QTextBrowser" name="textBrowser"><property name="geometry"><rect><x>40</x><y>160</y><width>351</width><height>271</height></rect></property></widget><widget class="QPushButton" name="pushButton"><property name="geometry"><rect><x>710</x><y>520</y><width>93</width><height>28</height></rect></property><property name="text"><string>开始</string></property></widget><widget class="QWidget" name=""><property name="geometry"><rect><x>450</x><y>160</y><width>251</width><height>271</height></rect></property><layout class="QVBoxLayout" name="verticalLayout"><item><widget class="QLineEdit" name="lineEdit"><property name="text"><string>姓名</string></property></widget></item><item><widget class="QDateTimeEdit" name="dateTimeEdit"/></item><item><widget class="QLineEdit" name="lineEdit_4"><property name="text"><string>身份证号</string></property></widget></item><item><widget class="QLineEdit" name="lineEdit_3"><property name="text"><string>12306的账号</string></property></widget></item><item><widget class="QLineEdit" name="lineEdit_2"><property name="text"><string>12306的密码</string></property></widget></item></layout></widget></widget><widget class="QMenuBar" name="menubar"><property name="geometry"><rect><x>0</x><y>0</y><width>800</width><height>26</height></rect></property></widget><widget class="QStatusBar" name="statusbar"/></widget><resources/><connections/>
</ui>

python爬虫(三)12306自动抢票--- selenium相关推荐

  1. 基于python的12306自动抢票系统的设计与实现

    铁路售票系统12306网站作为一个广受人们的日常使用工具,受大极大的关注.铁路售票的管理者都主要考虑降低成本,提升售票服务满意度.一年一度的春运和节假日出行高峰期,给众多的出行群众者带来了极大的烦恼, ...

  2. python github 12306 文贤平_GitHub - itsmartkit/12306-Ticket-Booking: 12306自动抢票系统(2020-01-10)...

    基于Python的12306自动订票系统 系统功能 1.余票监控:发现余票自动下单 2.自动打码:采用第三方免费接口/本地识别算法两种模式,自动验证图片验证码 3.小黑屋:发展有余票但是下单失败的车次 ...

  3. python 100行代码实现 12306 自动抢票

    基于Selenium和Chrome浏览器实现. 默认抢票类型为普通票,硬座.需求多的话可以在源码里改,我写的注释挺详细. 复制粘贴就能使用,2019年8月13日 测试可用. from selenium ...

  4. 12306自动抢票软件哪个好?记者亲测体验

    春运期间的火车票可谓一票难求,广大购票者抢票需求难以满足.在网上,铺天盖地的抢票插件也弄不清到底选哪家好.不要急,下面为大家介绍几款小编亲身实践过的功能强大的抢票软件,希望可以帮助大家顺利回家过年! ...

  5. 12306自动抢票及自动识别验证码功能(一)

    其实12306抢票之前有做过,近年来随着技术的发展AI的兴起,我也随波逐流,研究了下python深度学习,来实现12306全自动抢票工具. 1. 实现12306自动识别验证码,我这里用的比较简单,目前 ...

  6. python+selenium 12306自动抢票

    写在前面 又到了一年一度的春运,又要开始抢票了,因为并不是很相信XC.FZ等预约抢票,就想着自己写个脚本,但其实网上已经有很多人写了抢票程序,我为什么还要重写呢,因为12306的网页源代码是有变化的, ...

  7. Python实现12306自动抢票小程序

    项目描述: 本程序通过网络爬虫技术,通过抓包分析出一次购票过程中出现的所有请求,最后通过Python程序一步步实现模拟浏览器进行请求.本程序通过调用云打码平台实现登录时候验证码校验,并能实现不断地监控 ...

  8. python实现12306自动抢票脚本-splinter结合chrome浏览器

    ---------------------------------------------------------------------------------------------------- ...

  9. 用Python实现12306自动抢票脚本,五一假期出行无忧!

    用python另一个抢票神器,你get到了吗? 2017年时间飞逝,转眼间距离2018年春节还有不到1个月的时间,还在为抢不到火车票发愁吗?作为程序员的我们撸一个抢票软件可好? ... 难以想象的数据 ...

最新文章

  1. 《游戏服务器的架构演进》阅读笔记
  2. ITK:提取矢量图像的分量/通道
  3. java写一个搜索引擎_搜索引擎—-Java实现一个简单的网络爬虫
  4. 第三次学JAVA再学不好就吃翔(part57)--StringBuffer和String的相互转换
  5. P2685 [TJOI2012]桥
  6. 项目周期一般多久_积木创意:影响小程序开发外包的周期因素有哪些?
  7. Oracle Sharding
  8. bzoj 3374: [Usaco2004 Mar]Special Serial Numbers 特殊编号
  9. 拉普拉斯变换公式表_工程数学中的积分变换的总结
  10. 省市区三级数据-MySQL
  11. python爬虫之scrapy入门
  12. HBase项目之微博系统
  13. RFC822邮件格式
  14. 电脑桌面的计算机图双击打不开,win10双击电脑图标打不开必须右键打开
  15. 红色警戒2修改器原理百科(六)
  16. 用Python计算利率,告诉你亏了多少!
  17. Polychain重仓的Findora公链,想带领DeFi脱虚向实
  18. android WIFI学习总结
  19. 神策数据付力力:新一代的营销数据平台
  20. MySQL常见数据类型(小胖虎带你了解MySQL基础知识,只为博君一关注)

热门文章

  1. 截取嵌入网页的一部分
  2. Vue3.x 父组件Setup、Ref操纵子组件中的元素方法
  3. 小学数学与计算机整合课优质教案,小学数学与信息技术整合教案
  4. value_counts的ascending是干什么的
  5. 医生投书媒体曝光医院黑幕 “制度导致乱收费”
  6. IOS 学习笔记 Toolbar NavigationBar 导航栏 工具栏
  7. sql2000无法打开1433端口及解决方法
  8. 四.linunx mongodb远程连接
  9. 【阿里云服务器Ubuntu数据库MongoDB设置远程链接】
  10. java计算器实验报告前言_java计算器实验报告引言.doc