春秋航班自动查询脚本

 缘起于要订9月30日的机票回家。结果发现现在还太早,不能预定。又不想错过。只得每天上午下午各看两次。非常麻烦而且容易忘记。

于是写了一个python脚本自己查询,如果发现9月30日的可以预订了,就给我自己发封邮件通知,半小时自动查询一次。呵呵。

To those who want to use it:
代码中的邮件地址,服务器地址,端口号,用户名,口令需要替换成自己的真实情况
网页中的匹配条件也可以根据自己的修改。

我写来用掉就扔的,也没讲究什么结构。
 1 import sys
2 import os
3 import urllib2
4 import time
5
6 def sendmail(date) :
7 import smtplib
8
9 # Import the email modules we'll need
10 from email.MIMEText import MIMEText
11
12 # Create a text/plain message
13 msg = MIMEText("")
14
15 me = "xxx@xxx.com"
16 you = ["yyy@yyy.com"]
17 msg['Subject'] = date + "'flight of www.china-sss.com is available."
18 msg['From'] = me
19 msg['To'] = you[0]
20
21 # Send the message via our own SMTP server, but don't include the
22 # envelope header.
23 s = smtplib.SMTP()
24 s.connect("xxx.xxx.xxx.xxx:25")
25 s.login("xxx", "*****")
26 s.sendmail(me, you, msg.as_string())
27 s.quit()
28
29
30 while 1 :
31 proxy_handler = urllib2.ProxyHandler({'http': 'http://proxy:8080/'})
32 proxy_auth_handler = urllib2.HTTPBasicAuthHandler()
33
34 opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
35 urllib2.install_opener(opener)
36
37 # This time, rather than install the OpenerDirector, we use it directly:
38 f = urllib2.urlopen('http://www.china-sss.com/')
39
40 content = f.readlines()
41 foundflag = False
42 flightflag = False
43
44 if len(sys.argv) == 1 :
45 date = ["2006-9-26","2006-9-30"]
46 else :
47 date = sys.argv[1:]
48
49 for eachline in content :
50 if eachline.find("FLY_CITY=='CGD' && ARRIVE_CITY=='SHA'") != -1 :
51 foundflag = True
52 continue
53 if foundflag == True:
54 foundflag = False
55 for eachdate in date:
56 if eachline.find(eachdate) != -1 :
57 flightflag = True
58 print "Flight is available."
59 sendmail(eachdate)
60
61 if not flightflag :
62 print "Flight is not available yet."
63
64 time.sleep(1800)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值