|
@@ -8,6 +8,7 @@
|
|
|
|
|
|
'''
|
|
|
import argparse,time,json
|
|
|
+import base64
|
|
|
from email.mime.text import MIMEText
|
|
|
from email.header import Header
|
|
|
import os,sys,re,configparser,logging,requests
|
|
@@ -21,6 +22,9 @@ class Crawl12306:
|
|
|
self.args=self.get_args()
|
|
|
self.conf=self.get_conf()
|
|
|
|
|
|
+ def set_log(self):
|
|
|
+ logging.basicConfig()
|
|
|
+
|
|
|
def get_conf(self)->dict:
|
|
|
''' read config.ini and return a dict'''
|
|
|
cf = configparser.ConfigParser()
|
|
@@ -40,14 +44,33 @@ class Crawl12306:
|
|
|
''' get code picture '''
|
|
|
print("............... get code pic ...........")
|
|
|
for i in range(number):
|
|
|
+ print("-----------%s--------------" % (i))
|
|
|
+ # 设置代理
|
|
|
+ proxies = {
|
|
|
+ "http": "http://" + self.conf["proxy"]["proxy"],
|
|
|
+ "https": "https://" + self.conf["proxy"]["proxy"],
|
|
|
+ }
|
|
|
+ self.sess.proxies.update(proxies)
|
|
|
res= self.sess.get(api.pic_url)
|
|
|
time.sleep(random.randint(3,5))
|
|
|
if res.status_code == 200:
|
|
|
- with open("data/pic/pic_%s%s.png" % (int(time.time(),i),), "wb") as file:
|
|
|
+ with open("data/pic/pic_%s%s.png" % (int(time.time()), i), "wb") as file:
|
|
|
file.write(res.content)
|
|
|
|
|
|
- # 监控余票
|
|
|
+ def get_code_pic2(self, number=100):
|
|
|
+ ''' get code picture old api '''
|
|
|
+ print("............... get code pic ...........")
|
|
|
+ for i in range(number):
|
|
|
+ print("-----------%s--------------" % (i))
|
|
|
+ res= self.sess.get(api.pic_url2)
|
|
|
+ time.sleep(random.randint(1,3))
|
|
|
+ if res.status_code == 200:
|
|
|
+ img_str = json.loads(res.content)['image']
|
|
|
+ with open("data/pic2/pic_%s%s.png" % (int(time.time()), i), "wb") as file:
|
|
|
+ file.write(base64.b64decode(img_str))
|
|
|
+
|
|
|
def monitor(self):
|
|
|
+ ''' 监控余票 '''
|
|
|
print("余票监控中...")
|
|
|
while True:
|
|
|
try:
|