航空网站航班查询自动点击提醒器

前段时间国际飞机票非常难买,所以做了个小程序自动点击来监控机票网站(东方航空)的刷新。(虽然最后也没有刷到票,不过学习了一下简单的爬虫知识和发邮件的代码,改写后还能为自己使用,挺不错的)。
依赖环境:selenium,chrome driver,smtplib

用selenium中的by_css_selector来做的。在chrome中按F12进入开发者模式,有个小鼠标,将小鼠标对准某个元素,显示的蓝色字体部分就是css的定位码。 有时候网速太慢,需要通过sleep来提供足够的加载等待时间。
随后用smtplib来发邮件。(注意设置的代理邮箱里面需要设置POP3/SMTP服务的权限)

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium. webdriver.support.wait import WebDriverWait
import datetime
import time
from pyquery import PyQuery as pq
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.header import Header

def send_massage(happy_title,happy_news):
    # 1. 连接邮箱服务器
    con = smtplib.SMTP_SSL('smtp.qq.com', 465)

    # 2. 登录邮箱
    con.login('10001@qq.com', 'sgvvlcegqtihbdch')

    # 2. 准备数据
    # 创建邮件对象
    msg = MIMEMultipart()

    # 设置邮件主题
    subject = Header(happy_title, 'utf-8').encode()
    msg['Subject'] = subject

    # 设置邮件发送者
    msg['From'] = '10001@qq.com <100001@qq.com>'

    # 设置邮件接受者
    msg['To'] = 'emialget@163.com'

    # 添加文字内容
    text = MIMEText( happy_news, 'plain', 'utf-8')
    msg.attach(text)

    # 3.发送邮件
    con.sendmail('100001@qq.com', 'emialget@163.com', msg.as_string())
    print("已发送")
    con.quit()


def click_page():
    browser=webdriver.Chrome()
    url='https://global.ceair.com/'
    browser.get(url)
    for i in [19,26,33,47,18,25]:
        time.sleep(5)
        input_city=browser.find_elements_by_css_selector('input.ceair-input__inner.ceair-input__inner_homesearch')
        input_city[0].clear()
        input_city[0].send_keys('伦敦')
        time.sleep(5)
        a=browser.find_elements_by_css_selector('.search-result-airport .search-result-airport-1')
        time.sleep(1)
        a
        a[1].click()
        input_city[1].clear()
        input_city[1].send_keys('上海')
        time.sleep(5)
        b=browser.find_elements_by_css_selector('.search-result-airport .search-result-airport-1')
        b
        time.sleep(5)
        b[4].click()
        time_content=browser.find_element_by_css_selector(".fr.dateSelectBox .ceair-input__inner")
        time_content.click()
        time.sleep(5)
        time_button=browser.find_elements_by_css_selector(".days .cell")
        time_button[i].click()
        submit=browser.find_element_by_css_selector("button.ceair-button.submit-btn.ceair-button--danger")
        submit.click()
        time.sleep(20)
        html=browser.page_source
        doc=pq(html)
        dic=[]
        product={
                'start_time':doc('span.title-depart-time').text(),
                'time':doc('.detail-container.flex-col.al-center div.time-container').text(),
                'end_time':doc('div.title-arrive-time').text(),
                'status':doc('span.title-pre-buy').text()
                }

        if product['status'] == "客满":
            print(f"客满,现在是{i}")
        else:
            # happy_news = f'第{i-2}日出现了票!'
            # happy_title = '抢到票啦!有票刷新啦!快登入东航官网!'
            # send_massage(happy_title,happy_news)
            print(f"now is {i}:")
            print(product['status'])

        dic.append(product)
        browser.back()
        continue
    print('down')



    jsString = "document.querySelector('.fr.dateSelectBox .ceair-input__inner').removeAttribute('readonly')"
    browser.execute_script(jsString)
    browser.find_element_by_css_selector(".fr.dateSelectBox .ceair-input__inner").clear()
    browser.find_element_by_css_selector(".fr.dateSelectBox .ceair-input__inner").send_keys('2020-08-22')


if __name__ == "__main__" :
    cnt = 48
    times = 0
    news = '我会每半小时搜索一次官网!有消息立刻发邮件通知,如果没有消息的话也会6小时通知一次!'
    title = '东航搜索小助手已启动!'
    send_massage(title, news)
    while(cnt >= 0):
        times = times +1
        click_page()
        print(f"完成今日的第{times}次监控")
        time.sleep(1800)#半小时搜索一次
        if (times % 12 == 0):
            news = '0.0'
            title = '距离上次搜索已过去6小时,没有搜索到票'
            send_massage(title,news)



©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页