easyspider_executestage.py 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  1. # -*- coding: utf-8 -*-
  2. import atexit
  3. import io # 遇到错误退出时应执行的代码
  4. import json
  5. from lib2to3.pgen2 import driver
  6. import re
  7. import subprocess
  8. import sys
  9. from urllib import parse
  10. import base64
  11. import hashlib
  12. import time
  13. import requests
  14. from selenium.webdriver.chrome.options import Options
  15. from selenium.webdriver.common.keys import Keys
  16. from selenium.webdriver.common.action_chains import ActionChains
  17. from selenium import webdriver
  18. from selenium.webdriver.support.ui import WebDriverWait
  19. from selenium.webdriver.support import expected_conditions as EC
  20. from selenium.webdriver.common.by import By
  21. from selenium.common.exceptions import NoSuchElementException
  22. from selenium.common.exceptions import TimeoutException
  23. from selenium.common.exceptions import StaleElementReferenceException
  24. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  25. import random
  26. # import numpy
  27. import csv
  28. import os
  29. from selenium.webdriver.common.by import By
  30. from commandline_config import Config
  31. import pytesseract
  32. from PIL import Image
  33. saveName, log, OUTPUT, browser, SAVED = None, "", "", None, False
  34. desired_capabilities = DesiredCapabilities.CHROME
  35. desired_capabilities["pageLoadStrategy"] = "none"
  36. outputParameters = {}
  37. class Time:
  38. def __init__(self, type1=""):
  39. self.t = int(round(time.time() * 1000))
  40. self.type = type1
  41. def end(self):
  42. at = int(round(time.time() * 1000))
  43. Log(str(self.type)+":"+str(at-self.t))
  44. # 记录log
  45. def recordLog(str=""):
  46. global log
  47. log = log + str + "\n"
  48. # 控制台打印log函数
  49. def Log(text, text2=""):
  50. switch = False
  51. if switch:
  52. print(text, text2)
  53. # 屏幕滚动函数
  54. def scrollDown(para, rt=""):
  55. try:
  56. if para["scrollType"] != 0 and para["scrollCount"] > 0: # 控制屏幕向下滚动
  57. for i in range(para["scrollCount"]):
  58. time.sleep(1) # 下拉完等1秒
  59. Log("Wait for 1 second after screen scrolling")
  60. body = browser.find_element(By.CSS_SELECTOR, "body")
  61. if para["scrollType"] == 1:
  62. body.send_keys(Keys.PGDN)
  63. else:
  64. body.send_keys(Keys.END)
  65. except TimeoutException:
  66. Log('time out after 10 seconds when scrolling. ')
  67. recordLog('time out after 10 seconds when scrolling')
  68. browser.execute_script('window.stop()')
  69. if para["scrollType"] != 0 and para["scrollCount"] > 0: # 控制屏幕向下滚动
  70. for i in range(para["scrollCount"]):
  71. time.sleep(1) # 下拉完等1秒
  72. Log("Wait for 1 second after screen scrolling")
  73. body = browser.find_element(By.CSS_SELECTOR, "body")
  74. if para["scrollType"] == 1:
  75. body.send_keys(Keys.PGDN)
  76. else:
  77. body.send_keys(Keys.END)
  78. if rt != "":
  79. rt.end()
  80. def execute_code(codeMode, code, max_wait_time, element=None):
  81. output = ""
  82. if code == "":
  83. return ""
  84. if max_wait_time == 0:
  85. max_wait_time = 999999
  86. # print(codeMode, code)
  87. if int(codeMode) == 0:
  88. recordLog("Execute JavaScript:" + code)
  89. recordLog("执行JavaScript:" + code)
  90. browser.set_script_timeout(max_wait_time)
  91. try:
  92. output = browser.execute_script(code)
  93. except:
  94. output = ""
  95. recordLog("JavaScript execution failed")
  96. elif int(codeMode) == 2:
  97. recordLog("Execute JavaScript for element:" + code)
  98. recordLog("对元素执行JavaScript:" + code)
  99. browser.set_script_timeout(max_wait_time)
  100. try:
  101. output = browser.execute_script(code, element)
  102. except:
  103. output = ""
  104. recordLog("JavaScript execution failed")
  105. elif int(codeMode) == 1:
  106. recordLog("Execute System Call:" + code)
  107. recordLog("执行系统命令:" + code)
  108. # 执行系统命令,超时时间为5秒
  109. try:
  110. output = subprocess.run(code, capture_output=True, text=True, timeout=max_wait_time, encoding="utf-8")
  111. # 输出命令返回值
  112. output = output.stdout
  113. except subprocess.TimeoutExpired:
  114. # 命令执行时间超过5秒,抛出异常
  115. recordLog("Command timed out")
  116. recordLog("命令执行超时")
  117. except:
  118. recordLog("Command execution failed")
  119. recordLog("命令执行失败")
  120. return str(output)
  121. def customOperation(node, loopValue):
  122. paras = node["parameters"]
  123. codeMode = paras["codeMode"]
  124. code = paras["code"]
  125. max_wait_time = int(paras["waitTime"])
  126. output = execute_code(codeMode, code, max_wait_time)
  127. recordASField = paras["recordASField"]
  128. if recordASField:
  129. global OUTPUT, outputParameters
  130. outputParameters[node["title"]] = output
  131. line = []
  132. for value in outputParameters.values():
  133. line.append(value)
  134. print(value[:15], " ", end="")
  135. print("")
  136. OUTPUT.append(line)
  137. # 执行节点关键函数部分
  138. def executeNode(nodeId, loopValue="", clickPath="", index=0):
  139. node = procedure[nodeId]
  140. WebDriverWait(browser, 10).until
  141. # 等待元素出现才进行操作,10秒内未出现则报错
  142. (EC.visibility_of_element_located((By.XPATH, node["parameters"]["xpath"])))
  143. # 根据不同选项执行不同操作
  144. if node["option"] == 0 or node["option"] == 10: # root操作,条件分支操作
  145. for i in node["sequence"]: # 从根节点开始向下读取
  146. executeNode(i, loopValue, clickPath, index)
  147. elif node["option"] == 1: # 打开网页操作
  148. recordLog("openPage")
  149. openPage(node["parameters"], loopValue)
  150. elif node["option"] == 2: # 点击元素
  151. recordLog("Click")
  152. clickElement(node["parameters"], loopValue, clickPath, index)
  153. elif node["option"] == 3: # 提取数据
  154. recordLog("getData")
  155. getData(node["parameters"], loopValue, node["isInLoop"],
  156. parentPath=clickPath, index=index)
  157. saveData()
  158. elif node["option"] == 4: # 输入文字
  159. inputInfo(node["parameters"], loopValue)
  160. elif node["option"] == 5: # 自定义操作
  161. customOperation(node, loopValue)
  162. elif node["option"] == 8: # 循环
  163. recordLog("loop")
  164. loopExcute(node, loopValue, clickPath, index) # 执行循环
  165. elif node["option"] == 9: # 条件分支
  166. recordLog("judge")
  167. judgeExcute(node, loopValue, clickPath, index)
  168. # 执行完之后进行等待
  169. if node["option"] != 0:
  170. waitTime = 0.01 # 默认等待0.01秒
  171. if node["parameters"]["wait"] > 1:
  172. waitTime = node["parameters"]["wait"]
  173. time.sleep(waitTime)
  174. Log("Wait seconds after node executing: ", waitTime)
  175. # 对判断条件的处理
  176. def judgeExcute(node, loopElement, clickPath="", index=0):
  177. rt = Time("IF Condition")
  178. global bodyText # 引入bodyText
  179. executeBranchId = 0 # 要执行的BranchId
  180. for i in node["sequence"]:
  181. cnode = procedure[i] # 获得条件分支
  182. tType = int(cnode["parameters"]["class"]) # 获得判断条件类型
  183. if tType == 0: # 什么条件都没有
  184. executeBranchId = i
  185. break
  186. elif tType == 1: # 当前页面包含文本
  187. try:
  188. if bodyText.find(cnode["parameters"]["value"]) >= 0:
  189. executeBranchId = i
  190. break
  191. except: # 找不到元素下一个条件
  192. continue
  193. elif tType == 2: # 当前页面包含元素
  194. try:
  195. if browser.find_element(By.XPATH, cnode["parameters"]["value"]):
  196. executeBranchId = i
  197. break
  198. except: # 找不到元素或者xpath写错了,下一个条件
  199. continue
  200. elif tType == 3: # 当前循环元素包括文本
  201. try:
  202. if loopElement.text.find(cnode["parameters"]["value"]) >= 0:
  203. executeBranchId = i
  204. break
  205. except: # 找不到元素或者xpath写错了,下一个条件
  206. continue
  207. elif tType == 4: # 当前循环元素包括元素
  208. try:
  209. if loopElement.find_element(By.XPATH, cnode["parameters"]["value"][1:]):
  210. executeBranchId = i
  211. break
  212. except: # 找不到元素或者xpath写错了,下一个条件
  213. continue
  214. elif tType <= 7: # JS命令返回值
  215. if tType == 5: # JS命令返回值等于
  216. output = execute_code(0, cnode["parameters"]["code"], cnode["parameters"]["waitTime"])
  217. elif tType == 6: # System
  218. output = execute_code(1, cnode["parameters"]["code"], cnode["parameters"]["waitTime"])
  219. elif tType == 7: # 针对当前循环项的JS命令返回值
  220. output = execute_code(2, cnode["parameters"]["code"], cnode["parameters"]["waitTime"], loopElement)
  221. try:
  222. if output.find("rue") != -1: # 如果返回值中包含true
  223. code = 1
  224. else:
  225. code = int(output)
  226. except:
  227. code = 0
  228. if code > 0:
  229. executeBranchId = i
  230. break
  231. rt.end()
  232. if executeBranchId != 0:
  233. executeNode(executeBranchId, loopElement, clickPath, index)
  234. # 对循环的处理
  235. def loopExcute(node, loopValue, clickPath="", index=0):
  236. time.sleep(0.1) # 第一次执行循环的时候强制等待1秒
  237. # Log("循环执行前等待0.1秒")
  238. Log("Wait 0.1 second before loop")
  239. global history
  240. thisHandle = browser.current_window_handle # 记录本次循环内的标签页的ID
  241. thisHistoryLength = browser.execute_script(
  242. 'return history.length') # 记录本次循环内的history的length
  243. history["index"] = thisHistoryLength
  244. history["handle"] = thisHandle
  245. if int(node["parameters"]["loopType"]) == 0: # 单个元素循环
  246. # 无跳转标签页操作
  247. count = 0 # 执行次数
  248. while True: # do while循环
  249. try:
  250. finished = False
  251. element = browser.find_element(
  252. By.XPATH, node["parameters"]["xpath"])
  253. for i in node["sequence"]: # 挨个执行操作
  254. executeNode(i, element, node["parameters"]["xpath"], 0)
  255. finished = True
  256. Log("click: ", node["parameters"]["xpath"])
  257. recordLog("click:" + node["parameters"]["xpath"])
  258. except NoSuchElementException:
  259. # except:
  260. print("\n\n-------Get Element Error-------\n\n")
  261. Log("clickNotFound: ", node["parameters"]["xpath"])
  262. recordLog("clickNotFound:" + node["parameters"]["xpath"])
  263. for i in node["sequence"]: # 不带点击元素的把剩余的如提取数据的操作执行一遍
  264. if node["option"] != 2:
  265. executeNode(i, None, node["parameters"]["xpath"], 0)
  266. finished = True
  267. break # 如果找不到元素,退出循环
  268. finally:
  269. if not finished:
  270. print("\n\n-------Retrying-------\n\n")
  271. Log("-------Retrying-------: ",
  272. node["parameters"]["xpath"])
  273. recordLog("clickNotFound:" + node["parameters"]["xpath"])
  274. for i in node["sequence"]: # 不带点击元素的把剩余的如提取数据的操作执行一遍
  275. if node["option"] != 2:
  276. executeNode(i, None, node["parameters"]["xpath"], 0)
  277. break # 如果找不到元素,退出循环
  278. count = count + 1
  279. Log("Page: ", count)
  280. recordLog("Page:" + str(count))
  281. # print(node["parameters"]["exitCount"], "-------")
  282. if node["parameters"]["exitCount"] == count: # 如果达到设置的退出循环条件的话
  283. break
  284. elif int(node["parameters"]["loopType"]) == 1: # 不固定元素列表
  285. try:
  286. elements = browser.find_elements(By.XPATH,
  287. node["parameters"]["xpath"])
  288. for index in range(len(elements)):
  289. for i in node["sequence"]: # 挨个顺序执行循环里所有的操作
  290. executeNode(i, elements[index],
  291. node["parameters"]["xpath"], index)
  292. if browser.current_window_handle != thisHandle: # 如果执行完一次循环之后标签页的位置发生了变化
  293. while True: # 一直关闭窗口直到当前标签页
  294. browser.close() # 关闭使用完的标签页
  295. browser.switch_to.window(browser.window_handles[-1])
  296. if browser.current_window_handle == thisHandle:
  297. break
  298. if history["index"] != thisHistoryLength and history[
  299. "handle"] == browser.current_window_handle: # 如果执行完一次循环之后历史记录发生了变化,注意当前页面的判断
  300. difference = thisHistoryLength - \
  301. history["index"] # 计算历史记录变化差值
  302. browser.execute_script(
  303. 'history.go(' + str(difference) + ')') # 回退历史记录
  304. if node["parameters"]["historyWait"] > 2: # 回退后要等待的时间
  305. time.sleep(node["parameters"]["historyWait"])
  306. else:
  307. time.sleep(2)
  308. # 切换历史记录等待2秒或者:
  309. Log("Change history back time or:",
  310. node["parameters"]["historyWait"])
  311. browser.execute_script('window.stop()')
  312. except NoSuchElementException:
  313. Log("pathNotFound: ", node["parameters"]["xpath"])
  314. recordLog("pathNotFound: " + node["parameters"]["xpath"])
  315. pass # 循环中找不到元素就略过操作
  316. except Exception as e:
  317. raise
  318. elif int(node["parameters"]["loopType"]) == 2: # 固定元素列表
  319. for path in node["parameters"]["pathList"].split("\n"): # 千万不要忘了分割!!
  320. try:
  321. element = browser.find_element(By.XPATH, path)
  322. for i in node["sequence"]: # 挨个执行操作
  323. executeNode(i, element, path, 0)
  324. if browser.current_window_handle != thisHandle: # 如果执行完一次循环之后标签页的位置发生了变化
  325. while True: # 一直关闭窗口直到当前标签页
  326. browser.close() # 关闭使用完的标签页
  327. browser.switch_to.window(browser.window_handles[-1])
  328. if browser.current_window_handle == thisHandle:
  329. break
  330. if history["index"] != thisHistoryLength and history[
  331. "handle"] == browser.current_window_handle: # 如果执行完一次循环之后历史记录发生了变化,注意当前页面的判断
  332. difference = thisHistoryLength - \
  333. history["index"] # 计算历史记录变化差值
  334. browser.execute_script(
  335. 'history.go(' + str(difference) + ')') # 回退历史记录
  336. if node["parameters"]["historyWait"] > 2: # 回退后要等待的时间
  337. time.sleep(node["parameters"]["historyWait"])
  338. else:
  339. time.sleep(2)
  340. Log("Change history back time or:",
  341. node["parameters"]["historyWait"])
  342. browser.execute_script('window.stop()')
  343. except NoSuchElementException:
  344. Log("pathNotFound: ", path)
  345. recordLog("pathNotFound: " + path)
  346. continue # 循环中找不到元素就略过操作
  347. except Exception as e:
  348. raise
  349. elif int(node["parameters"]["loopType"]) == 3: # 固定文本列表
  350. textList = node["parameters"]["textList"].split("\n")
  351. for text in textList:
  352. recordLog("input: " + text)
  353. for i in node["sequence"]: # 挨个执行操作
  354. executeNode(i, text, "", 0)
  355. elif int(node["parameters"]["loopType"]) == 4: # 固定网址列表
  356. # tempList = node["parameters"]["textList"].split("\r\n")
  357. urlList = list(
  358. filter(isnull, node["parameters"]["textList"].split("\n"))) # 去空行
  359. # urlList = []
  360. # for url in tempList:
  361. # if url != "":
  362. # urlList.append(url)
  363. for url in urlList:
  364. recordLog("input: " + url)
  365. for i in node["sequence"]:
  366. executeNode(i, url, "", 0)
  367. elif int(node["parameters"]["loopType"]) <= 6: # 命令返回值
  368. while True: # do while循环
  369. if int(node["parameters"]["loopType"]) == 5: # JS
  370. output = execute_code(0, node["parameters"]["code"], node["parameters"]["waitTime"])
  371. elif int(node["parameters"]["loopType"]) == 6: # System
  372. output = execute_code(1, node["parameters"]["code"], node["parameters"]["waitTime"])
  373. try:
  374. if output.find("rue") != -1: # 如果返回值中包含true
  375. code = 1
  376. else:
  377. code = int(output)
  378. except:
  379. code = 0
  380. if code <= 0:
  381. break
  382. for i in node["sequence"]: # 挨个执行操作
  383. executeNode(i, code, node["parameters"]["xpath"], 0)
  384. history["index"] = thisHistoryLength
  385. history["handle"] = browser.current_window_handle
  386. scrollDown(node["parameters"])
  387. # 打开网页事件
  388. def openPage(para, loopValue):
  389. rt = Time("打开网页")
  390. time.sleep(2) # 打开网页后强行等待至少2秒
  391. time.sleep(random.uniform(1, 10)) # 生成一个a到b的小数等待时间
  392. global links
  393. global urlId
  394. global history
  395. global outputParameters
  396. # try:
  397. # firstTime = True
  398. # for handle in browser.window_handles:
  399. # browser.switch_to.window(handle)
  400. # if (not firstTime):
  401. # browser.close()
  402. # firstTime = False
  403. # except:
  404. # return
  405. if len(browser.window_handles) > 1:
  406. browser.switch_to.window(browser.window_handles[-1]) # 打开网页操作从第1个页面开始
  407. browser.close()
  408. browser.switch_to.window(browser.window_handles[0]) # 打开网页操作从第1个页面开始
  409. history["handle"] = browser.current_window_handle
  410. if para["useLoop"]:
  411. url = loopValue
  412. else:
  413. url = links[urlId]
  414. try:
  415. browser.get(url)
  416. Log('Loading page: ' + url)
  417. recordLog('Loading page: ' + url)
  418. except TimeoutException:
  419. Log('time out after 10 seconds when loading page: ' + url)
  420. recordLog('time out after 10 seconds when loading page: ' + url)
  421. browser.execute_script('window.stop()')
  422. rt.end()
  423. try:
  424. history["index"] = browser.execute_script("return history.length")
  425. except TimeoutException:
  426. browser.execute_script('window.stop()')
  427. history["index"] = browser.execute_script("return history.length")
  428. rt.end()
  429. scrollDown(para, rt) # 控制屏幕向下滚动
  430. if containJudge:
  431. global bodyText # 每次执行点击,输入元素和打开网页操作后,需要更新bodyText
  432. try:
  433. bodyText = browser.find_element(By.CSS_SELECTOR, "body").text
  434. Log('URL Page: ' + url)
  435. recordLog('URL Page: ' + url)
  436. except TimeoutException:
  437. Log('time out after 10 seconds when getting body text: ' + url)
  438. recordLog('time out after 10 seconds when getting body text:: ' + url)
  439. browser.execute_script('window.stop()')
  440. time.sleep(1)
  441. Log("Need to wait 1 second to get body text")
  442. # 再执行一遍
  443. bodyText = browser.find_element(By.CSS_SELECTOR, "body").text
  444. rt.end()
  445. except Exception as e:
  446. Log(e)
  447. recordLog(str(e))
  448. # clear output parameters
  449. for key in outputParameters:
  450. outputParameters[key] = ""
  451. rt.end()
  452. # 键盘输入事件
  453. def inputInfo(para, loopValue):
  454. time.sleep(1) # 输入之前等待1秒
  455. Log("Wait 1 second before input")
  456. rt = Time("Input Text")
  457. try:
  458. textbox = browser.find_element(By.XPATH, para["xpath"])
  459. except:
  460. Log("Cannot find input box element:" +
  461. para["xpath"] + "Please try to set the wait time before executing this operation")
  462. recordLog("Cannot find input box element:" +
  463. para["xpath"] + "Please try to set the wait time before executing this operation")
  464. # textbox.send_keys(Keys.CONTROL, 'a')
  465. # textbox.send_keys(Keys.BACKSPACE)
  466. execute_code(2, para["beforeJS"], para["beforeJSWaitTime"], textbox) # 执行前置JS
  467. # Send the HOME key
  468. textbox.send_keys(Keys.HOME)
  469. # Send the SHIFT + END key combination
  470. textbox.send_keys(Keys.SHIFT, Keys.END)
  471. # Send the DELETE key
  472. textbox.send_keys(Keys.DELETE)
  473. if para["useLoop"]:
  474. textbox.send_keys(loopValue)
  475. else:
  476. textbox.send_keys(para["value"])
  477. execute_code(2, para["afterJS"], para["afterJSWaitTime"], textbox) # 执行后置js
  478. global bodyText # 每次执行点击,输入元素和打开网页操作后,需要更新bodyText
  479. bodyText = browser.find_element(By.CSS_SELECTOR, "body").text
  480. rt.end()
  481. # 点击元素事件
  482. def clickElement(para, loopElement=None, clickPath="", index=0):
  483. global history
  484. time.sleep(0.1) # 点击之前等待1秒
  485. rt = Time("Click Element")
  486. Log("Wait 1 second before clicking element")
  487. if para["useLoop"]: # 使用循环的情况下,传入的clickPath就是实际的xpath
  488. path = clickPath
  489. else:
  490. path = para["xpath"] # 不然使用元素定义的xpath
  491. # 点击前对该元素执行一段JavaScript代码
  492. try:
  493. if para["beforeJS"] != "":
  494. element = browser.find_element(By.XPATH, path)
  495. execute_code(2, para["beforeJS"], para["beforeJSWaitTime"], element)
  496. except:
  497. Log("Cannot find element:" +
  498. path + "Please try to set the wait time before executing this operation")
  499. recordLog("Cannot find element:" +
  500. path + "Please try to set the wait time before executing this operation")
  501. tempHandleNum = len(browser.window_handles) # 记录之前的窗口位置
  502. try:
  503. script = 'var result = document.evaluate(`' + path + \
  504. '`, document, null, XPathResult.ANY_TYPE, null);for(let i=0;i<arguments[0];i++){result.iterateNext();} result.iterateNext().click();'
  505. browser.execute_script(script, str(index)) # 用js的点击方法
  506. except TimeoutException:
  507. Log('time out after 10 seconds when loading clicked page')
  508. recordLog('time out after 10 seconds when loading clicked page')
  509. browser.execute_script('window.stop()')
  510. rt.end()
  511. except Exception as e:
  512. Log(e)
  513. recordLog(str(e))
  514. time.sleep(0.5) # 点击之后等半秒
  515. Log("Wait 0.5 second after clicking element")
  516. time.sleep(random.uniform(1, 3)) # 生成一个a到b的小数等待时间
  517. # 点击前对该元素执行一段JavaScript代码
  518. try:
  519. if para["afterJS"] != "":
  520. element = browser.find_element(By.XPATH, path)
  521. execute_code(2, para["afterJS"], para["afterJSWaitTime"], element)
  522. except:
  523. Log("Cannot find element:" +
  524. path + "Please try to set the wait time before executing this operation")
  525. recordLog("Cannot find element:" +
  526. path + "Please try to set the wait time before executing this operation")
  527. if tempHandleNum != len(browser.window_handles): # 如果有新标签页的行为发生
  528. browser.switch_to.window(browser.window_handles[-1]) # 跳转到新的标签页
  529. history["handle"] = browser.current_window_handle
  530. try:
  531. history["index"] = browser.execute_script("return history.length")
  532. except TimeoutException:
  533. browser.execute_script('window.stop()')
  534. history["index"] = browser.execute_script("return history.length")
  535. rt.end()
  536. else:
  537. try:
  538. history["index"] = browser.execute_script("return history.length")
  539. except TimeoutException:
  540. browser.execute_script('window.stop()')
  541. history["index"] = browser.execute_script("return history.length")
  542. rt.end()
  543. # 如果打开了新窗口,切换到新窗口
  544. scrollDown(para, rt) # 根据参数配置向下滚动
  545. if containJudge: # 有判断语句才执行以下操作
  546. global bodyText # 每次执行点击,输入元素和打开网页操作后,需要更新bodyText
  547. try:
  548. bodyText = browser.find_element(By.CSS_SELECTOR, "body").text
  549. except TimeoutException:
  550. Log('time out after 10 seconds when getting body text')
  551. recordLog('time out after 10 seconds when getting body text')
  552. browser.execute_script('window.stop()')
  553. time.sleep(1)
  554. Log("wait one second after get body text")
  555. # 再执行一遍
  556. bodyText = browser.find_element(By.CSS_SELECTOR, "body").text
  557. rt.end()
  558. except Exception as e:
  559. Log(e)
  560. recordLog(str(e))
  561. rt.end()
  562. # 提取数据事件
  563. def getData(para, loopElement, isInLoop=True, parentPath="", index=0):
  564. if not isInLoop and para["wait"] == 0:
  565. time.sleep(1) # 如果提取数据字段不在循环内而且设置的等待时间为0,默认等待1秒
  566. Log("Wait 1 second before extracting data")
  567. rt = Time("Extract Data")
  568. for p in para["paras"]:
  569. content = ""
  570. if not (p["contentType"] == 5 or p["contentType"] == 6): # 如果不是页面标题或URL,去找元素
  571. try:
  572. if p["relative"]: # 是否相对xpath
  573. if p["relativeXPath"] == "": # 相对xpath有时候就是元素本身,不需要二次查找
  574. element = loopElement
  575. else:
  576. if p["relativeXPath"].find("//") >= 0: # 如果字串里有//即子孙查找,则不动语句
  577. full_path = "(" + parentPath + \
  578. p["relativeXPath"] + ")" + \
  579. "[" + str(index + 1) + "]"
  580. element = browser.find_element(By.XPATH, full_path)
  581. else:
  582. element = loopElement.find_element(By.XPATH,
  583. p["relativeXPath"][1:])
  584. else:
  585. element = browser.find_element(By.XPATH, p["relativeXPath"])
  586. except NoSuchElementException: # 找不到元素的时候,使用默认值
  587. # print(p)
  588. try:
  589. content = p["default"]
  590. except Exception as e:
  591. content = ""
  592. outputParameters[p["name"]] = content
  593. Log('Element %s not found, use default' % p["relativeXPath"])
  594. recordLog('Element %s not found, use default' % p["relativeXPath"])
  595. continue
  596. except TimeoutException: # 超时的时候设置超时值
  597. Log('time out after 10 seconds when getting data')
  598. recordLog('time out after 10 seconds when getting data')
  599. browser.execute_script('window.stop()')
  600. if p["relative"]: # 是否相对xpath
  601. if p["relativeXPath"] == "": # 相对xpath有时候就是元素本身,不需要二次查找
  602. element = loopElement
  603. else:
  604. element = loopElement.find_element(By.XPATH,
  605. p["relativeXPath"][1:])
  606. else:
  607. element = browser.find_element(By.XPATH, p["relativeXPath"])
  608. rt.end()
  609. try:
  610. execute_code(2, p["beforeJS"], p["beforeJSWaitTime"], element) # 执行前置js
  611. if p["contentType"] == 2:
  612. content = element.get_attribute('innerHTML')
  613. elif p["contentType"] == 3:
  614. content = element.get_attribute('outerHTML')
  615. elif p["contentType"] == 4:
  616. # 获取元素的背景图片地址
  617. bg_url = element.value_of_css_property('background-image')
  618. # 清除背景图片地址中的多余字符
  619. bg_url = bg_url.replace('url("', '').replace('")', '')
  620. content = bg_url
  621. elif p["contentType"] == 5:
  622. content = browser.current_url
  623. elif p["contentType"] == 6:
  624. content = browser.title
  625. elif p["contentType"] == 7:
  626. # 获取整个网页的高度和宽度
  627. height = browser.execute_script("return document.body.scrollHeight");
  628. width = browser.execute_script("return document.body.scrollWidth");
  629. # 调整浏览器窗口的大小
  630. browser.set_window_size(width, height)
  631. element.screenshot("Data/" +saveName + "/"+ str(time.time()) + ".png")
  632. elif p["contentType"] == 8:
  633. try:
  634. screenshot = element.screenshot_as_png
  635. screenshot_stream = io.BytesIO(screenshot)
  636. # 使用Pillow库打开截图,并转换为灰度图像
  637. image = Image.open(screenshot_stream).convert('L')
  638. # 使用Tesseract OCR引擎识别图像中的文本
  639. text = pytesseract.image_to_string(image, lang='chi_sim+eng')
  640. content = text
  641. except Exception as e:
  642. content = "OCR失败"
  643. print("To use OCR, You need to install Tesseract-OCR and add it to the environment variable PATH: https://tesseract-ocr.github.io/tessdoc/Installation.html")
  644. print("要使用OCR识别功能,你需要安装Tesseract-OCR并将其添加到环境变量PATH中:https://blog.csdn.net/u010454030/article/details/80515501")
  645. elif p["contentType"] == 9:
  646. content = execute_code(2, p["JS"], p["JSWaitTime"], element)
  647. elif p["contentType"] == 1: # 只采集当期元素下的文本,不包括子元素
  648. command = 'var arr = [];\
  649. var content = arguments[0];\
  650. for(var i = 0, len = content.childNodes.length; i < len; i++) {\
  651. if(content.childNodes[i].nodeType === 3){ \
  652. arr.push(content.childNodes[i].nodeValue);\
  653. }\
  654. }\
  655. var str = arr.join(" "); \
  656. return str;'
  657. content = browser.execute_script(command, element).replace(
  658. "\n", "").replace("\\s+", " ")
  659. if p["nodeType"] == 2:
  660. if element.get_attribute("href") != None:
  661. content = element.get_attribute("href")
  662. else:
  663. content = ""
  664. elif p["nodeType"] == 3:
  665. if element.get_attribute("value") != None:
  666. content = element.get_attribute("value")
  667. else:
  668. content = ""
  669. elif p["nodeType"] == 4: # 图片
  670. if element.get_attribute("src") != None:
  671. content = element.get_attribute("src")
  672. else:
  673. content = ""
  674. elif p["contentType"] == 0:
  675. content = element.text
  676. if p["nodeType"] == 2:
  677. if element.get_attribute("href") != None:
  678. content = element.get_attribute("href")
  679. else:
  680. content = ""
  681. elif p["nodeType"] == 3:
  682. if element.get_attribute("value") != None:
  683. content = element.get_attribute("value")
  684. else:
  685. content = ""
  686. elif p["nodeType"] == 4: # 图片
  687. if element.get_attribute("src") != None:
  688. content = element.get_attribute("src")
  689. else:
  690. content = ""
  691. except StaleElementReferenceException: # 发生找不到元素的异常后,等待几秒重新查找
  692. recordLog('StaleElementReferenceException:'+p["relativeXPath"])
  693. time.sleep(3)
  694. try:
  695. if p["relative"]: # 是否相对xpath
  696. if p["relativeXPath"] == "": # 相对xpath有时候就是元素本身,不需要二次查找
  697. element = loopElement
  698. recordLog('StaleElementReferenceException:loopElement')
  699. else:
  700. element = loopElement.find_element(By.XPATH,
  701. p["relativeXPath"][1:])
  702. recordLog(
  703. 'StaleElementReferenceException:loopElement+relativeXPath')
  704. else:
  705. element = browser.find_element(
  706. By.XPATH, p["relativeXPath"])
  707. recordLog('StaleElementReferenceException:relativeXPath')
  708. if p["contentType"] == 2:
  709. content = element.get_attribute('innerHTML')
  710. elif p["contentType"] == 3:
  711. content = element.get_attribute('outerHTML')
  712. elif p["contentType"] == 4:
  713. # 获取元素的背景图片地址
  714. bg_url = element.value_of_css_property('background-image')
  715. # 清除背景图片地址中的多余字符
  716. bg_url = bg_url.replace('url("', '').replace('")', '')
  717. content = bg_url
  718. elif p["contentType"] == 5:
  719. content = browser.current_url
  720. elif p["contentType"] == 6:
  721. content = browser.title
  722. elif p["contentType"] == 7:
  723. # 获取整个网页的高度和宽度
  724. height = browser.execute_script("return document.body.scrollHeight");
  725. width = browser.execute_script("return document.body.scrollWidth");
  726. # 调整浏览器窗口的大小
  727. browser.set_window_size(width, height)
  728. element.screenshot("Data/" +saveName + "/"+ str(time.time()) + ".png")
  729. elif p["contentType"] == 8:
  730. try:
  731. screenshot = element.screenshot_as_png
  732. screenshot_stream = io.BytesIO(screenshot)
  733. # 使用Pillow库打开截图,并转换为灰度图像
  734. image = Image.open(screenshot_stream).convert('L')
  735. # 使用Tesseract OCR引擎识别图像中的文本
  736. text = pytesseract.image_to_string(image, lang='chi_sim+eng')
  737. content = text
  738. except Exception as e:
  739. content = "OCR失败"
  740. print("To use OCR, You need to install Tesseract-OCR and add it to the environment variable path: https://tesseract-ocr.github.io/tessdoc/Installation.html")
  741. print("要使用OCR识别功能,你需要安装Tesseract-OCR并将其添加到环境变量path中:")
  742. elif p["contentType"] == 9:
  743. content = execute_code(2, p["JS"], p["JSWaitTime"], element)
  744. elif p["contentType"] == 1: # 只采集当期元素下的文本,不包括子元素
  745. command = 'var arr = [];\
  746. var content = arguments[0];\
  747. for(var i = 0, len = content.childNodes.length; i < len; i++) {\
  748. if(content.childNodes[i].nodeType === 3){ \
  749. arr.push(content.childNodes[i].nodeValue);\
  750. }\
  751. }\
  752. var str = arr.join(" "); \
  753. return str;'
  754. content = browser.execute_script(command, element).replace(
  755. "\n", "").replace("\\s+", " ")
  756. if p["nodeType"] == 2:
  757. if element.get_attribute("href") != None:
  758. content = element.get_attribute("href")
  759. else:
  760. content = ""
  761. elif p["nodeType"] == 3:
  762. if element.get_attribute("value") != None:
  763. content = element.get_attribute("value")
  764. else:
  765. content = ""
  766. elif p["nodeType"] == 4: # 图片
  767. if element.get_attribute("src") != None:
  768. content = element.get_attribute("src")
  769. else:
  770. content = ""
  771. elif p["contentType"] == 0:
  772. content = element.text
  773. if p["nodeType"] == 2:
  774. if element.get_attribute("href") != None:
  775. content = element.get_attribute("href")
  776. else:
  777. content = ""
  778. elif p["nodeType"] == 3:
  779. if element.get_attribute("value") != None:
  780. content = element.get_attribute("value")
  781. else:
  782. content = ""
  783. elif p["nodeType"] == 4: # 图片
  784. if element.get_attribute("src") != None:
  785. content = element.get_attribute("src")
  786. else:
  787. content = ""
  788. except StaleElementReferenceException:
  789. recordLog('StaleElementReferenceException:'+p["relativeXPath"])
  790. continue # 再出现类似问题直接跳过
  791. outputParameters[p["name"]] = content
  792. execute_code(2, p["afterJS"], p["afterJSWaitTime"], element) # 执行后置JS
  793. global OUTPUT
  794. line = []
  795. for value in outputParameters.values():
  796. line.append(value)
  797. print(value[:15], " ", end="")
  798. print("")
  799. OUTPUT.append(line)
  800. rt.end()
  801. # 判断字段是否为空
  802. def isnull(s):
  803. return len(s) != 0
  804. def saveData(exit=False):
  805. global saveName, log, OUTPUT, browser
  806. if exit == True or len(OUTPUT) > 100: # 每100条保存一次
  807. with open("Data/"+saveName + '_log.txt', 'a', encoding='utf-8-sig') as file_obj:
  808. file_obj.write(log)
  809. file_obj.close()
  810. with open("Data/"+saveName + '.csv', 'a', encoding='utf-8-sig', newline="") as f:
  811. f_csv = csv.writer(f)
  812. for line in OUTPUT:
  813. f_csv.writerow(line)
  814. f.close()
  815. OUTPUT = []
  816. log = ""
  817. @atexit.register
  818. def clean():
  819. global saveName, log, OUTPUT, browser, SAVED
  820. saveData(exit=True)
  821. browser.quit()
  822. sys.exit(saveName + '.csv')
  823. if __name__ == '__main__':
  824. config = {
  825. "id": 0,
  826. "server_address": "http://localhost:8074",
  827. "saved_file_name": "",
  828. "read_type": "remote",
  829. "user_data": False,
  830. "config_folder": "",
  831. "config_name": "config.json",
  832. "headless": False,
  833. }
  834. c = Config(config)
  835. print(c)
  836. options = Options()
  837. driver_path = "chromedriver.exe"
  838. import platform
  839. print(sys.platform, platform.architecture())
  840. option = webdriver.ChromeOptions()
  841. if not os.path.exists(os.getcwd()+"/Data"):
  842. os.mkdir(os.getcwd()+"/Data")
  843. if sys.platform == "darwin" and platform.architecture()[0] == "64bit":
  844. options.binary_location = "EasySpider.app/Contents/Resources/app/chrome_mac64.app/Contents/MacOS/Google Chrome"
  845. # MacOS需要用option而不是options!
  846. option.binary_location = "EasySpider.app/Contents/Resources/app/chrome_mac64.app/Contents/MacOS/Google Chrome"
  847. driver_path = "EasySpider.app/Contents/Resources/app/chromedriver_mac64"
  848. # options.binary_location = "chrome_mac64.app/Contents/MacOS/Google Chrome"
  849. # # MacOS需要用option而不是options!
  850. # option.binary_location = "chrome_mac64.app/Contents/MacOS/Google Chrome"
  851. # driver_path = os.getcwd()+ "/chromedriver_mac64"
  852. print(driver_path)
  853. elif os.path.exists(os.getcwd()+"/EasySpider/resources"): # 打包后的路径
  854. print("Finding chromedriver in EasySpider",
  855. os.getcwd()+"/EasySpider")
  856. if sys.platform == "win32" and platform.architecture()[0] == "32bit":
  857. options.binary_location = os.path.join(
  858. os.getcwd(), "EasySpider/resources/app/chrome_win32/chrome.exe") # 指定chrome位置
  859. driver_path = os.path.join(
  860. os.getcwd(), "EasySpider/resources/app/chrome_win32/chromedriver_win32.exe")
  861. elif sys.platform == "win32" and platform.architecture()[0] == "64bit":
  862. options.binary_location = os.path.join(
  863. os.getcwd(), "EasySpider/resources/app/chrome_win64/chrome.exe")
  864. driver_path = os.path.join(
  865. os.getcwd(), "EasySpider/resources/app/chrome_win64/chromedriver_win64.exe")
  866. elif sys.platform == "linux" and platform.architecture()[0] == "64bit":
  867. options.binary_location = "EasySpider/resources/app/chrome_linux64/chrome"
  868. driver_path = "EasySpider/resources/app/chrome_linux64/chromedriver_linux64"
  869. else:
  870. print("Unsupported platform")
  871. sys.exit()
  872. print("Chrome location:", options.binary_location)
  873. print("Chromedriver location:", driver_path)
  874. elif os.path.exists(os.getcwd()+"/../ElectronJS"):
  875. if os.getcwd().find("ElectronJS") >= 0: # 软件dev用
  876. print("Finding chromedriver in EasySpider",
  877. os.getcwd())
  878. option.binary_location = "chrome_win64/chrome.exe"
  879. driver_path = "chrome_win64/chromedriver_win64.exe"
  880. else: # 直接在executeStage文件夹内使用python easyspider_executestage.py时的路径
  881. print("Finding chromedriver in EasySpider",
  882. os.getcwd()+"/ElectronJS")
  883. option.binary_location = "../ElectronJS/chrome_win64/chrome.exe" # 指定chrome位置
  884. driver_path = "../ElectronJS/chrome_win64/chromedriver_win64.exe"
  885. elif os.getcwd().find("ExecuteStage") >= 0: # 如果直接执行
  886. print("Finding chromedriver in ./Chrome",
  887. os.getcwd()+"/Chrome")
  888. options.binary_location = "./Chrome/chrome.exe" # 指定chrome位置
  889. # option.binary_location = "C:\\Users\\q9823\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe"
  890. driver_path = "./Chrome/chromedriver.exe"
  891. else:
  892. options.binary_location = "./chrome.exe" # 指定chrome位置
  893. driver_path = "./chromedriver.exe"
  894. option.add_experimental_option(
  895. 'excludeSwitches', ['enable-automation']) # 以开发者模式
  896. # user_data_dir = r'' # 注意没有Default!
  897. # options.add_argument('--user-data-dir='+p)
  898. # 总结:
  899. # 0. 带Cookie需要用userdatadir
  900. # 1. chrome_options才是配置用户文件和chrome文件地址的正确选项
  901. # 2. User Profile文件夹的路径是:C:\Users\用户名\AppData\Local\Google\Chrome\User Data不要加Default
  902. # 3. 就算User Profile相同,chrome版本不同所存储的cookie信息也不同,也不能爬
  903. # 4. TMALL如果一直弹出验证码,而且无法通过验证,那么需要在其他浏览器上用
  904. if c.user_data:
  905. with open(c.config_folder + c.config_name,"r", encoding='utf-8') as f:
  906. config = json.load(f)
  907. absolute_user_data_folder = config["absolute_user_data_folder"]
  908. print("\nAbsolute_user_data_folder:",absolute_user_data_folder,"\n")
  909. option.add_argument(f'--user-data-dir={absolute_user_data_folder}') # TMALL 反扒
  910. option.add_argument("--profile-directory=Default")
  911. if c.headless:
  912. print("Headless mode")
  913. print("无头模式")
  914. option.add_argument("--headless")
  915. options.add_argument("--headless")
  916. # options.add_argument(
  917. # '--user-data-dir=C:\\Users\\q9823\\AppData\\Local\\Google\\Chrome\\User Data') # TMALL 反扒
  918. option.add_argument(
  919. "--disable-blink-features=AutomationControlled") # TMALL 反扒
  920. options.add_argument("--disable-blink-features=AutomationControlled") # TMALL 反扒
  921. print(options)
  922. browser = webdriver.Chrome(
  923. options=options, chrome_options=option, executable_path=driver_path)
  924. stealth_path = driver_path[:driver_path.find("chromedriver")] + "stealth.min.js"
  925. with open(stealth_path, 'r') as f:
  926. js = f.read()
  927. print("Loading stealth.min.js")
  928. browser.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', {'source': js}) # TMALL 反扒
  929. wait = WebDriverWait(browser, 10)
  930. browser.get('about:blank')
  931. browser.set_page_load_timeout(10) # 加载页面最大超时时间
  932. browser.set_script_timeout(10)
  933. id = c.id
  934. print("id: ", id)
  935. if c.saved_file_name != "":
  936. saveName = "task_" + str(id) + "_" + c.saved_file_name # 保存文件的名字
  937. else:
  938. saveName = "task_" + str(id) + "_" + \
  939. str(random.randint(0, 999999999)) # 保存文件的名字
  940. print("saveName: ", saveName)
  941. os.mkdir("Data/" + saveName) # 创建保存文件夹用来保存截图
  942. backEndAddress = c.server_address
  943. if c.read_type == "remote":
  944. print("remote")
  945. content = requests.get(backEndAddress + "/queryExecutionInstance?id=" + str(id))
  946. service = json.loads(content.text) # 加载服务信息
  947. else:
  948. print("local")
  949. with open("execution_instances/" + str(id) + ".json", 'r', encoding='utf-8') as f:
  950. content = f.read()
  951. service = json.loads(content) # 加载服务信息
  952. print("name: ", service["name"])
  953. procedure = service["graph"] # 程序执行流程
  954. links = list(filter(isnull, service["links"].split("\n"))) # 要执行的link的列表
  955. OUTPUT = [] # 采集的数据
  956. OUTPUT.append([]) # 添加表头
  957. containJudge = service["containJudge"] # 是否含有判断语句
  958. bodyText = "" # 记录bodyText
  959. tOut = service["outputParameters"] # 生成输出参数对象
  960. outputParameters = {}
  961. log = "" # 记下现在总共开了多少个标签页
  962. history = {"index": 0, "handle": None} # 记录页面现在所以在的历史记录的位置
  963. SAVED = False # 记录是否已经存储了
  964. for para in tOut:
  965. outputParameters[para["name"]] = ""
  966. OUTPUT[0].append(para["name"])
  967. # 挨个执行程序
  968. urlId = 0 # 全局记录变量
  969. for i in range(len(links)):
  970. executeNode(0)
  971. urlId = urlId + 1
  972. files = os.listdir("Data/" + saveName)
  973. # 如果目录为空,则删除该目录
  974. if not files:
  975. os.rmdir("Data/" + saveName)
  976. print("Done!")
  977. print("执行完成!")
  978. recordLog("Done!")
  979. # dataPath = os.path.abspath(os.path.join(os.getcwd(), "../Data"))
  980. # with open("Data/"+saveName + '_log.txt', 'a', encoding='utf-8-sig') as file_obj:
  981. # file_obj.write(log)
  982. # file_obj.close()
  983. # with open("Data/"+saveName + '.csv', 'a', encoding='utf-8-sig', newline="") as f:
  984. # f_csv = csv.writer(f)
  985. # for line in OUTPUT:
  986. # f_csv.writerow(line)
  987. # f.close()
  988. saveData()
  989. browser.quit()