缘起于要订9月30日的机票回家。结果发现现在还太早,不能预定。又不想错过。只得每天上午下午各看两次。非常麻烦而且容易忘记。
于是写了一个python脚本自己查询,如果发现9月30日的可以预订了,就给我自己发封邮件通知,半小时自动查询一次。呵呵。
To those who want to use it:
代码中的邮件地址,服务器地址,端口号,用户名,口令需要替换成自己的真实情况
网页中的匹配条件也可以根据自己的修改。
我写来用掉就扔的,也没讲究什么结构。
于是写了一个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)