0


python爬虫(三)12306自动抢票--- selenium

往期目录

一、

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模块+配置+可能报错解释

win + r 打开cmd

然后输入 pip install selenuim 下载selenuim

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

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

然后下载谷歌驱动 chomedriver 下载

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

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

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

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

import time
from selenium import webdriver

driver = 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 tabulate

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])  #点击完会打开新的页面 然后要跳转到新的页面去开始我们下一步的操作 没有的话你会卡在第一个窗口 后续代码运行不了
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 tabulate

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])  #点击完会打开新的页面 然后要跳转到新的页面去开始我们下一步的操作 没有的话你会卡在第一个窗口 后续代码运行不了
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}]').text
            if k == '':
                pass
            else:
                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 sleep

class 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 爬虫 selenium

本文转载自: https://blog.csdn.net/weixin_64050468/article/details/130792919
版权归原作者 暴风雨544 所有, 如有侵权,请联系我们删除。

“python爬虫(三)12306自动抢票--- selenium”的评论:

还没有评论