monitor_chrome.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import sys
  2. from selenium import webdriver
  3. import urllib
  4. from selenium.webdriver.chrome.service import Service
  5. from selenium.webdriver.common.by import By
  6. import os
  7. import time
  8. def process_one():
  9. global driver
  10. googleurl = 'https://www.google.com/search?q={}&num={}&hl={}'.format(urllib.parse.quote('風起'), 100,'zh-TW')
  11. print(googleurl)
  12. driver.get(googleurl)
  13. while True:
  14. try:
  15. elmts = driver.find_elements(By.XPATH, "//div[@class='yuRUbf']/a")
  16. print('尋找')
  17. break
  18. except:
  19. pass
  20. print('搜尋結果數量',len(elmts))
  21. n = 0
  22. n_run = 0
  23. if len(elmts) == 0:
  24. n+=1
  25. print('異常次數',n)
  26. os.system('python3 reboot.py')
  27. else:
  28. print('正常')
  29. n_run+=1
  30. print('執行次數:',n_run)
  31. def run_once(pport):
  32. global driver
  33. s = Service('/root/driver/chromedriver')
  34. user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36 OPR/68.0.3618.125"
  35. options = webdriver.ChromeOptions()
  36. options.add_argument('--headless')
  37. options.add_argument('--remote-debugging-port=9222')
  38. options.add_experimental_option("debuggerAddress", f"127.0.0.1:{pport}")
  39. options.add_argument("--user-agent=" + user_agent)
  40. options.add_argument("--incognito")
  41. driver = webdriver.Chrome(options=options, service=s)
  42. driver.delete_all_cookies()
  43. driver.set_window_size(1400, 1000)
  44. process_one()
  45. time.sleep(3)
  46. driver.quit()
  47. try:
  48. os.system('docker container restart tiny10')
  49. time.sleep(1)
  50. run_once(9929)
  51. except:
  52. os.system('docker container restart tiny9')
  53. time.sleep(15)