-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathseleniumDriver.py
674 lines (513 loc) · 22.5 KB
/
seleniumDriver.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
#!/usr/local/bin/python
# -*- coding: utf8 -*-
'''
Created on 2016年9月2日
@author: zyq
'''
import traceback
import urlparse
# from telnetlib import EC
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.proxy import ProxyType
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
import PIL.Image as image
import time,re,cStringIO,urllib2
import random
from Config import minPIPCount, USER_AGENTS, phantomPath, phantomBrowserNames
from proxy.ipproxy import getAvailableIPs, deletByIP
keyStamp = [1486693909951,1486693910245,1486693910638,1486693912697,1486693913091,1486693914631,1486693915013,1486693915313,1486693916550,1486693917430,1486693917799,1486693919750]
keyS2 = []
for ind in range(0,len(keyStamp)):
if ind == 0:
keyS2.append(0)
else:
keyS2.append(keyStamp[ind] - keyStamp[ind - 1])
def tradMarkTest(word):
driver = webdriver.PhantomJS(
executable_path=r"/Users/zyq/projects/simpleCrawler/phantomjs-2.1.1-macosx/bin/phantomjs")
# driver = webdriver.Firefox()
# 打开网页
baseUrl = 'http://202.108.90.73/txnS01.do'
driver.get( baseUrl)
for category in range(1, 46):
if category > 1:
driver.refresh()
catInputTag = driver.find_element_by_css_selector('#nc')
catInputTag.send_keys(category)
nameInputTag = driver.find_element_by_css_selector('#mn')
nameInputTag.send_keys(word)
submitTag = driver.find_element_by_css_selector('#_searchButton')
submitTag.click()
print 'before submit url', driver.current_url
# time.sleep(0.1)
windowsHandler = driver.window_handles
driver.switch_to.window(windowsHandler[1])
print 'after submit switch to new tab, url', driver.current_url
list_box = driver.find_element_by_css_selector('.list_box')
if not list_box:
print 'no result list, content: ',driver.page_source
continue
resList = list_box.find_elements_by_css_selector('tr')
resultLength = len(resList)
print 'result count:' , resultLength - 1
if resultLength == 2:
print driver.page_source
for resultIndx in range(1, resultLength):
resTr = resList[resultIndx]
for link in resTr.find_elements_by_css_selector('a'):
link.click()
windowsHandler = driver.window_handles
driver.switch_to.window(windowsHandler[2])
# linkUr = urlparse.urljoin(baseUrl, link['href'])
print 'walking result urls , now :', driver.current_url
# driver.get(linkUr)
print 'deatail page length: ', len(driver.page_source)
datas = []
for info in driver.find_elements_by_css_selector('.info'):
if(len(info.text) < 20):
datas.append(info.text)
print 'datas:',repr(datas).decode("unicode-escape")
print 'close current detail windows'
driver.close()
print 'switch to results page'
driver.switch_to.window(windowsHandler[1])
break
# driver.quit()
driver.close()
# windowsHandler = driver.window_handles
driver.switch_to.window(windowsHandler[0])
print 'finish categoty:', category, ' reflash search page'
# driver.quit()
def tradMarkTestById( f, t):
# from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
# driver = webdriver.PhantomJS(executable_path=phantomPath,service_args=service_args)
# caps = webdriver.DesiredCapabilities.PHANTOMJS
ua = random.choice(USER_AGENTS)
# caps["phantomjs.page.settings.userAgent"] = ua
pIPs = getAvailableIPs()
print 'start with ' + str(len(pIPs)) + ' proxy ips'
# startWithDriver(driver, f, t)
baseUrl = 'http://202.108.90.73/txnS03.do'
count = 0
startTime = time.time()
lastCountTime = time.time()
# pIPs, pIp, randomPIpIndex = reflashProxy(caps, driver, pIPs)
pipOk = False
maxCountPerProxy = 5
nowCount = 0
driver = None
noNeedReStart = False
for category in range(f, t + 1):
if not pipOk or nowCount > maxCountPerProxy:
# pIPs, pIp, randomPIpIndex = reflashProxy(caps, driver, pIPs)
if len(pIPs) < minPIPCount:
# 代理ip太少,重新获取
pIPs = getAvailableIPs()
pipObj = random.choice(pIPs)
# randomPIpIndex = random.randint(0, len(pIPs))
# pipObj = pIPs[randomPIpIndex]
pIp = pipObj[0]
pPort = pipObj[1]
# ua = random.choice(USER_AGENTS)
# caps["phantomjs.page.settings.userAgent"] = ua
# proxy = webdriver.Proxy()
# proxy.proxy_type = ProxyType.MANUAL
# proxy.http_proxy = pIp + ':' + str(pPort)
# # 将代理设置添加到webdriver.DesiredCapabilities.PHANTOMJS中
# proxy.add_to_capabilities(caps)
# driver.start_session(caps)
if driver:
try:
# driver.close()
driver.quit()
except Exception as er:
print er
caps = webdriver.DesiredCapabilities.PHANTOMJS
ua = random.choice(USER_AGENTS)
caps["phantomjs.page.settings.userAgent"] = ua
service_args = [
'--proxy=' + pIp + ':' + str(pPort),
'--proxy-type=http',
]
noNeedReStart = False
nowCount = 0
else:
nowCount = nowCount + 1
try:
if not noNeedReStart:
driver = webdriver.PhantomJS(executable_path=phantomPath, service_args=service_args, desired_capabilities=caps)
driver.set_page_load_timeout(30)
# driver.set_window_size(1366,768)
driver.get(baseUrl)
else:
driver.refresh()
pipOk = True
catInputTag = driver.find_element_by_css_selector('.inputbox input')
catInputTag.send_keys(category)
# nameInputTag = driver.find_element_by_css_selector('#mn')
# nameInputTag.send_keys(word)
submitTag = driver.find_element_by_css_selector('#_searchButton')
submitTag.click()
# print 'before submit url', driver.current_url
time.sleep(0.1)
windowsHandler = driver.window_handles
if len(windowsHandler) < 2:
print 'not open result page, skip'
noNeedReStart = True
continue
driver.switch_to.window(windowsHandler[1])
# print 'after submit switch to new tab, url', driver.current_url
list_box = driver.find_element_by_css_selector('.list_box')
if not list_box:
print 'no result list, content: ', driver.page_source
noNeedReStart = True
continue
resList = list_box.find_elements_by_css_selector('tr')
resultLength = len(resList)
print 'result count:', resultLength - 1
# if resultLength == 2:
# print driver.page_source
for resultIndx in range(1, resultLength):
resTr = resList[resultIndx]
for link in resTr.find_elements_by_css_selector('a'):
link.click()
windowsHandler = driver.window_handles
if len(windowsHandler) < 2:
print 'not open result page, skip'
noNeedReStart = True
continue
driver.switch_to.window(windowsHandler[2])
# linkUr = urlparse.urljoin(baseUrl, link['href'])
print 'walking result urls , now :', driver.current_url
# driver.get(linkUr)
# print 'deatail page length: ', len(driver.page_source)
# datas = []
# for info in driver.find_elements_by_css_selector('.info'):
# if(len(info.text) < 20):
# datas.append(info.text)
# print 'datas:',repr(datas).decode("unicode-escape")
count = count + 1
if count % 10 == 0:
spentTime = time.time() - startTime
print 'count: ', count, ' took ', spentTime, ' seconds, avg_per_sec: ', count / spentTime, \
' last 10 spent ', (time.time() - lastCountTime), ' secs'
lastCountTime = time.time()
# print 'close current detail windows'
# driver.close()
try:
driver.close()
# driver.quit()
except Exception as er:
print er
print 'switch to results page'
driver.switch_to.window(windowsHandler[1])
try:
driver.close()
# driver.quit()
except Exception as er:
print er
break
# driver.quit()
# driver.close()
# windowsHandler = driver.window_handles
# randomTime = random.randint(500, )
# time.sleep(randomTime / 100)
driver.switch_to.window(windowsHandler[0])
# driver.close()
# driver.quit()
# try:
# driver.close()
# # driver.quit()
# except Exception as er:
# print er
# try:
# # driver.close()
# driver.quit()
# except Exception as er:
# print er
noNeedReStart = True
print 'finish categoty:', category, ' reflash search page'
except Exception as e:
print 'id: ', category, ' error:', e
print 'proxy ip:',pIp,' port:',pPort
# randomTime = random.randint(100, 500)
# time.sleep(randomTime / 100)
# print 'source: ', driver.page_source
# driver.quit()
try:
driver.close()
# driver.quit()
except Exception as er:
print er
try:
# driver.close()
driver.quit()
except Exception as er:
print er
noNeedReStart = False
# del pIPs[randomPIpIndex]
pIPs.remove(pipObj)
# deletByIP(pIp)
pipOk = False
# try:
# driver.get(baseUrl)
# except Exception as e:
# print 'reopen baseUrl fail:', e
def reflashProxy(caps, driver, pIPs):
if len(pIPs) < minPIPCount:
# 代理ip太少,重新获取
pIPs = getAvailableIPs()
# pipObj = random.choice(pIPs)
randomPIpIndex = random.randint(0, len(pIPs))
pipObj = pIPs[randomPIpIndex]
pIp = pipObj[0]
pPort = pipObj[1]
ua = random.choice(USER_AGENTS)
caps["phantomjs.page.settings.userAgent"] = ua
proxy = webdriver.Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = pIp + ':' + str(pPort)
# 将代理设置添加到webdriver.DesiredCapabilities.PHANTOMJS中
proxy.add_to_capabilities(caps)
driver.start_session(caps)
return pIPs, pIp, randomPIpIndex
def shangbiaoOneIP(f, t):
caps = webdriver.DesiredCapabilities.PHANTOMJS
ua = random.choice(USER_AGENTS)
caps["phantomjs.page.settings.userAgent"] = ua
driver = webdriver.PhantomJS(executable_path=phantomPath, desired_capabilities=caps)
driver.set_page_load_timeout(30)
# driver.set_window_size(1366,768)
# driver.get(baseUrl)
# oldStartWithDriver(driver, f, t)
def getPaths():
filePaths = open('mousePaths')
paths = []
startStamp = -1
while 1:
line = filePaths.readline()
if not line:
break
point = []
line = line.replace('\n', '')
pos = line.split(',')
point.append( pos[0])
point.append( pos[1])
# point[1] = pos[1]
stamp = pos[2]
if startStamp < 0:
startStamp = stamp
# point[2] = stamp - startStamp
point.append(long(stamp) - long(startStamp))
startStamp = stamp
paths.append(point)
return paths
def getPaths2():
filePaths = open('mousePaths2')
paths = []
startStamp = -1
while 1:
line = filePaths.readline()
if not line:
break
point = []
line = line.replace('\n', '')
pos = line.split(',')
point.append(pos[0])
point.append(pos[1])
# point[1] = pos[1]
stamp = pos[2]
if startStamp < 0:
startStamp = stamp
# point[2] = stamp - startStamp
point.append(long(stamp) - long(startStamp))
startStamp = stamp
paths.append(point)
return paths
def oldStartWithDriver( f, t):
# caps = webdriver.DesiredCapabilities.PHANTOMJS
# ua = random.choice(USER_AGENTS)
# ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36'
#
# headers = {
# 'Accept': '*/*',
# # 'Accept-Encoding': 'gzip, deflate',
# 'Accept-Language': 'en-US,en,zh_CN;q=0.8',
# 'Connection': 'keep-alive',
# 'Cache-Control': 'max-age=0'
# }
#
# for key, value in headers.iteritems():
# caps['phantomjs.page.customHeaders.{}'.format(key)] = value
# caps["phantomjs.page.settings.userAgent"] = ua
# ranBrow = random.choice(phantomBrowserNames)
# caps["browserName"] = ranBrow
# caps['platform'] = ua
# caps['version'] =
# driver = webdriver.PhantomJS(executable_path=phantomPath, desired_capabilities=caps)
driver = webdriver.Chrome(executable_path='/Users/zyq/env/chromedriver')
# driver.set_page_load_timeout(30)
baseUrl = 'http://202.108.90.73/txnS03.do'
# baseUrl = 'file:///Users/zyq/projects/crawl/trademark/%E5%95%86%E6%A0%87%E7%8A%B6%E6%80%81%E6%A3%80%E7%B4%A22.html'
count = 0
startTime = time.time()
lastCountTime = time.time()
driver.get(baseUrl)
paths = getPaths()
paths2 = getPaths2()
# driver.
# time.sleep(600)
for category in range(f, t + 1):
try:
if category > f:
driver.refresh()
y = 600
# for track in [random.randint(800, 1366)]:
# track_string = track_string + "{%d,%d}," % (track, y - 445)
# # xoffset=track+22:这里的移动位置的值是相对于滑动圆球左上角的相对值,而轨迹变量里的是圆球的中心点,所以要加上圆球长度的一半。
# # yoffset=y-445:这里也是一样的。不过要注意的是不同的浏览器渲染出来的结果是不一样的,要保证最终的计算后的值是22,也就是圆球高度的一半
# ActionChains(driver).move_to_element_with_offset( xoffset=track + 22,
# yoffset=y - 445).perform()
# # 间隔时间也通过随机函数来获得
# time.sleep(random.randint(10, 50) / 100)
# time.sleep(200)
# catInputTag = driver.find_element_by_css_selector('.inputbox input')
catInputTag = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".inputbox input")))
inputPos = catInputTag.location
print 'dest1:',inputPos
inputX = inputPos['x']
inputY = inputPos['y']
inputX = inputPos['x']
inputY = inputPos['y']
for point in paths:
# xoff = int(point[0]) - 100
# yoff = int(point[1]) - 17
xoff = int(point[0])
yoff = int(point[1])
sleepTime = point[2]
print 'now pos: ',str(xoff),str(yoff),'offset: ',str(inputX - xoff),str(inputY - yoff)
ActionChains(driver).move_to_element_with_offset(catInputTag,inputX - xoff, inputY - yoff).perform()
time.sleep(sleepTime / float(1000))
# ActionChains(driver).move_to_element(catInputTag).perform()
time.sleep(random.random())
# act = ActionChains(driver).click(catInputTag).perform()
ActionChains(driver).click_and_hold(catInputTag).perform()
time.sleep(0.9)
ActionChains(driver).release(catInputTag).perform()
time.sleep(random.randint(1, 10) / float(1000))
# time.sleep(60)
# for c in str(category):
for i in range(0, len(str(category))):
c = str(category)[i]
# act.send_keys(c)
catInputTag.send_keys(c)
time.sleep(keyS2[i + 1] / float(1000))
# catInputTag.send_keys(category)
# nameInputTag = driver.find_element_by_css_selector('#mn')
# nameInputTag.send_keys(word)
# time.sleep(random.randint(0.1,2))
# time.sleep(random.random() * 2)
# time.sleep(0.5)
# time.sleep(600)
# submitTag = driver.find_element_by_css_selector('#_searchButton')
submitTag = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#_searchButton")))
inputPos = submitTag.location
print 'dest1:',inputPos
inputX2 = inputPos['x']
inputY2 = inputPos['y']
for point in paths2:
xoff = int(point[0])
yoff = int(point[1])
sleepTime = point[2]
print 'now pos: ',str(xoff),str(yoff),'offset: ',str(inputX2 - xoff),str(inputY2 - yoff)
ActionChains(driver).move_to_element_with_offset(submitTag, inputX2 - xoff, inputY2 - yoff).perform()
time.sleep(sleepTime / float(1000))
# ActionChains(driver).move_to_element(submitTag).perform()
# ActionChains(driver).move_to_element(catInputTag)
time.sleep(random.randint(1, 10) / float(1000))
# time.sleep(600)
# submitTag.click()
ActionChains(driver).click_and_hold(submitTag).perform()
time.sleep(0.8)
ActionChains(driver).release(submitTag).perform()
# ActionChains(driver).click().perform()
# print 'before submit url', driver.current_url
# time.sleep(random.random() )
time.sleep(random.randint(1, 10) / float(1000))
time.sleep(600)
windowsHandler = driver.window_handles
driver.switch_to.window(windowsHandler[1])
# print 'after submit switch to new tab, url', driver.current_url
list_box = driver.find_element_by_css_selector('.list_box')
if not list_box:
print 'no result list, content: ', driver.page_source
continue
resList = list_box.find_elements_by_css_selector('tr')
resultLength = len(resList)
print 'result count:', resultLength - 1
# if resultLength == 2:
# print driver.page_source
# time.sleep(random.random())
# time.sleep(random.random())
for resultIndx in range(1, resultLength):
resTr = resList[resultIndx]
for link in resTr.find_elements_by_css_selector('a'):
link.click()
# time.sleep(random.randint(0.1, 1))
time.sleep(random.random())
# time.sleep(1)
windowsHandler = driver.window_handles
driver.switch_to.window(windowsHandler[2])
# linkUr = urlparse.urljoin(baseUrl, link['href'])
print u'爬取成功, 最终链接为:', driver.current_url
# driver.get(linkUr)
# print 'deatail page length: ', len(driver.page_source)
# datas = []
# for info in driver.find_elements_by_css_selector('.info'):
# if(len(info.text) < 20):
# datas.append(info.text)
# print 'datas:',repr(datas).decode("unicode-escape")
count = count + 1
if count % 10 == 0:
spentTime = time.time() - startTime
print u'共成功: ', count, u' 次, 花费 ', spentTime, u' 秒, 平均一秒: ', count / spentTime, \
u'个, 最后10个花费: ', (time.time() - lastCountTime), u' 秒'
lastCountTime = time.time()
# print 'close current detail windows'
driver.close()
print 'switch to results page'
driver.switch_to.window(windowsHandler[1])
break
# driver.quit()
driver.close()
# windowsHandler = driver.window_handles
# randomTime = random.randint(50, 200)
# time.sleep(randomTime / 100)
# time.sleep(random.random())
driver.switch_to.window(windowsHandler[0])
print 'finish categoty:', category, ' reflash search page'
except Exception as e:
print 'id: ', category, u' 爬取失败:', traceback.format_exc()
# print 'update size exception, ', traceback.format_exc()
# if driver:
# print driver.page_source
# randomTime = random.randint(100, 500)
# time.sleep(randomTime / 100)
driver.quit()
driver = webdriver.PhantomJS(executable_path=phantomPath, desired_capabilities=caps)
driver.set_page_load_timeout(30)
try:
driver.get(baseUrl)
except Exception as e:
print 'reopen baseUrl fail:', e
# print 'source: ', driver.page_source
# driver.quit()
if __name__ == '__main__':
pass
# paths = getPaths()
# main()
# tradMarkTestById(19540000, 19550000)
oldStartWithDriver(19530000, 19540000)