forked from Moonlor/ctrip_spider_frame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpiderWork.py
305 lines (241 loc) · 13 KB
/
SpiderWork.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from multiprocessing.managers import BaseManager
from selenium.webdriver.chrome.options import Options
import requests
import json
import time
import random
import datetime
import re
import pymysql
import multiprocessing
from selenium import webdriver
from RandomUserAgent import RandomUserAgent
global null, false, true
null = ''
false = 0
true = 1
class SpiderWork(object):
def __init__(self):
#初始化分布式进程中的工作节点的连接工作
# 实现第一步:使用BaseManager注册获取Queue的方法名称
BaseManager.register('get_task_queue')
BaseManager.register('get_result_queue')
self.totoal_count = 0
# 实现第二步:连接到服务器:
server_addr = '127.0.0.1'
print('Connect to server %s...' % server_addr)
# 端口和验证口令注意保持与服务进程设置的完全一致:
self.m = BaseManager(address=(server_addr, 8011), authkey=b'woshinibaba')
# 从网络连接:
self.m.connect()
# 实现第三步:获取Queue的对象:
self.task = self.m.get_task_queue()
self.result = self.m.get_result_queue()
self.fail_flag = 0
print('init finish')
def store_data(self, r, con, cur):
for i in r["fltitem"]:
flight_id = i["mutilstn"][0]["basinfo"]["flgno"]
airline = i["mutilstn"][0]["basinfo"]["airsname"]
model = i["mutilstn"][0]["craftinfo"]["cname"] + i["mutilstn"][0]["craftinfo"]["craft"]
dept_date, dept_time = i["mutilstn"][0]["dateinfo"]["ddate"].split(' ')
dept_city = i["mutilstn"][0]["dportinfo"]["cityname"]
dept_airport = i["mutilstn"][0]["dportinfo"]["aportsname"] + i["mutilstn"][0]["dportinfo"]["bsname"]
arv_date, arv_time = i["mutilstn"][0]["dateinfo"]["adate"].split(' ')
arv_city = i["mutilstn"][0]["aportinfo"]["cityname"]
arv_airport = i["mutilstn"][0]["aportinfo"]["aportsname"] + i["mutilstn"][0]["aportinfo"]["bsname"]
isstop = i["mutilstn"][0]["isstop"]
try:
if isstop == 1:
tran_city = i["mutilstn"][0]["fsitem"][0]["city"]
tran_arvdate, tran_arvtime = i["mutilstn"][0]["fsitem"][0]["arrtime"].split(' ')
tran_depdate, tran_deptime = i["mutilstn"][0]["fsitem"][0]["deptime"].split(' ')
else:
tran_city = ""
tran_arvdate, tran_arvtime = "0001-01-01", ""
tran_depdate, tran_deptime = "0001-01-01", ""
except:
tran_city = ""
tran_arvdate, tran_arvtime = "0001-01-01", ""
tran_depdate, tran_deptime = "0001-01-01", ""
try:
flight_id = i["mutilstn"][0]["basinfo"]["flgno"] + '|' + i["mutilstn"][1]["basinfo"]["flgno"]
airline = i["mutilstn"][0]["basinfo"]["airsname"] + '|' + i["mutilstn"][1]["basinfo"]["airsname"]
model = i["mutilstn"][0]["craftinfo"]["cname"] + i["mutilstn"][0]["craftinfo"]["craft"] + '|' + \
i["mutilstn"][1]["craftinfo"]["cname"] + i["mutilstn"][1]["craftinfo"]["craft"]
tran_city = arv_city
arv_city = i["mutilstn"][1]["dportinfo"]["cityname"]
arv_airport = i["mutilstn"][1]["aportinfo"]["aportsname"] + i["mutilstn"][1]["aportinfo"]["bsname"]
tran_arvdate, tran_arvtime = arv_date, arv_time
tran_depdate, tran_deptime = i["mutilstn"][1]["dateinfo"]["ddate"].split(' ')
arv_date, arv_time = i["mutilstn"][1]["dateinfo"]["adate"].split(' ')
isstop = 1
except:
pass
flight_day = i["fltoday"]
ontime_Rate = 0
try:
for j in i["mutilstn"][0]["comlist"]:
if j["type"] == 2:
ontime_Rate = j["stip"]
except:
pass
price_1, price_2, price_3 = 99999, 99999, 99999
for k in i["policyinfo"]:
if re.match("经济舱", k["classinfor"][0]["display"]):
price_1 = k["tprice"]
if re.match(".*公务舱", k["classinfor"][0]["display"]):
price_2 = k["tprice"]
if re.match(".*头等舱", k["classinfor"][0]["display"]):
price_3 = k["tprice"]
date = time.strftime("%Y_%m_%d", time.localtime())
cur.execute(
' INSERT INTO Flight_%s' % date + '(airline,flight_id,model,dept_date,dept_time,dept_city,dept_airport,arv_date,arv_time,arv_city,arv_airport,isstop,tran_city,tran_arvdate,tran_arvtime,tran_depdate,tran_deptime,flight_day,ontime_Rate,price_1,price_2,price_3) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
(airline, flight_id, model, dept_date, dept_time, dept_city, dept_airport, arv_date, arv_time, arv_city,
arv_airport, isstop, tran_city, tran_arvdate, tran_arvtime, tran_depdate, tran_deptime, flight_day,
ontime_Rate, price_1, price_2, price_3))
con.commit()
def crawler(self, dcity, acity, dtime, cid, con, cur, headers, pipe2):
payload = json.dumps({"preprdid": "","trptpe": 1,"flag": 8,"searchitem": [{"dccode": "%s" % dcity, "accode": "%s" % acity, "dtime": "%s" % dtime}],"version": [{"Key": "170710_fld_dsmid", "Value": "Q"}],"head": {"cid": "%s" % cid, "ctok": "", "cver": "1.0", "lang": "01", "sid": "8888","syscode": "09", "auth": 'null',"extension": [{"name": "protocal", "value": "https"}]},"contentType": "json"})
print("正在使用cid : " + cid)
header = {'User-Agent': headers.head()}
tmp = requests.post('https://sec-m.ctrip.com/restapi/soa2/11781/Domestic/Swift/FlightList/Query?_fxpcqlniredt=' + cid, data=payload, headers=header)
r = eval(tmp.content.decode('utf-8'))
print(r)
if (self.totoal_count + 1 % 9) == 0:
pipe2.send('refresh')
try:
self.store_data(r, con, cur)
print('成功爬取' + ' ' + dcity + ' ' + acity + ' ' + dtime)
self.totoal_count += 1
print(self.totoal_count)
# proxies.valid_IP.add(ip)
except (Exception) as e:
print(e)
print('服务器繁忙' + ' ' + dcity + ' ' + acity + ' ' + dtime)
pipe2.send('refresh')
time.sleep(random.random() * 10 + 60)
self.fail_flag = self.fail_flag + 1
def camouflage_broewser(self, pipe, date, d_city, a_city):
search_date = (datetime.date.today() + datetime.timedelta(days= 60)).strftime("%Y-%m-%d")
url = 'https://m.ctrip.com/html5/flight/swift/domestic/' + d_city + '/' + a_city + '/' + search_date
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
# driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(2)
print('Waiting...')
driver.get(url)
print('Waiting...')
# driver.find_element_by_class_name('page-back-button').click()
try:
driver.find_elements_by_class_name('page-back-button')[-1].click()
except (Exception) as e:
print(e)
for i in range(2):
time.sleep(3)
driver.execute_script('window.scrollTo(0,document.body.scrollHeight)')
driver.find_element_by_class_name('button-primary').click()
remote_cookies = driver.get_cookies()
local_cookies = {}
for each in remote_cookies:
print(each)
name = each['name']
value = each['value']
local_cookies[name] = value
local_cookies['MKT_Pagesource'] = 'H5'
pipe.send(local_cookies)
while True:
msg = pipe.recv()
if msg == 'ok':
driver.quit()
print("浏览器关闭,单条航线爬取完毕")
return
if msg == 'refresh':
driver.find_element_by_class_name('day').click()
driver.quit()
def mainWork(self, date, d_city, a_city, cookies, con, cur, pipe2):
headers = RandomUserAgent()
cid = cookies['GUID']
self.crawler(d_city, a_city, date, cid, con, cur, headers, pipe2)
def crawl(self):
pip = multiprocessing.Pipe()
pipe1 = pip[0]
pipe2 = pip[1]
print("爬虫进程开始运行")
while (True):
try:
if not self.task.empty():
airline = self.task.get()
con = pymysql.connect(host='111.231.143.45', user='papa', passwd='woshinibaba', db='flight',
port=3306,
charset='utf8')
cur = con.cursor()
table_date = time.strftime("%Y_%m_%d", time.localtime())
try:
cur.execute('''CREATE TABLE Flight_%s''' % table_date + ''' (
airline varchar(255) NOT NULL,
flight_id varchar(255) NOT NULL,
model varchar(255) NOT NULL,
dept_date date NOT NULL,
dept_time varchar(255) NOT NULL,
dept_city varchar(255) NOT NULL,
dept_airport varchar(255) NOT NULL,
arv_date date NOT NULL,
arv_time varchar(255) NOT NULL,
arv_city varchar(255) NOT NULL,
arv_airport varchar(255) NOT NULL,
isstop float NOT NULL,
tran_city varchar(255) NOT NULL,
tran_arvdate date NOT NULL,
tran_arvtime varchar(255) NOT NULL,
tran_depdate date NOT NULL,
tran_deptime varchar(255) NOT NULL,
flight_day float NOT NULL,
ontime_Rate float NOT NULL,
price_1 float NOT NULL,
price_2 float NOT NULL,
price_3 float NOT NULL
);''')
except:
print("已经存在数据库")
if airline == 'end':
print('控制节点通知爬虫节点停止工作...')
# 接着通知其它节点停止工作
# self.result.put({'confirmed_airline': 'end', 'data': 'end'})
return
print('get: <<<<<<<<' + airline + '>>>>>>>>>>>')
target = airline.split('|')
date_list = []
for i in range(180):
date_list.append((datetime.date.today() + datetime.timedelta(days=i + 1)).strftime("%Y-%m-%d"))
d_city = target[1]
a_city = target[2]
browser_proc = multiprocessing.Process(target = self.camouflage_broewser, args = (pipe1, date_list[0], d_city, a_city))
browser_proc.start()
cookie = pipe2.recv()
for i in range(len(date_list)):
print('爬虫节点正在解析: 旅行日期 %s | 出发城市 %s | 到达城市 %s' % (date_list[i], d_city, a_city))
self.mainWork(date_list[i], d_city, a_city, cookie,con ,cur, pipe2)
time.sleep(random.random() + 4)
if self.fail_flag > 5:
break
pipe2.send('ok')
browser_proc.join()
print("浏览器已经关闭,线程同步")
if self.fail_flag > 5:
self.result.put(airline)
print("[!]通知控制节点重新爬取: " + airline)
self.fail_flag = 0
except (EOFError) as e:
print("连接工作节点失败")
return
except (Exception) as e:
print(e)
print('Crawl fali ')
if __name__=="__main__":
spider = SpiderWork()
print("连接成功")
spider.crawl()