101bots.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from selenium import webdriver
  2. import time
  3. from selenium.webdriver.support.ui import WebDriverWait
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support import expected_conditions as EC
  6. from selenium.webdriver.common.keys import Keys
  7. from selenium.webdriver.chrome.service import Service
  8. import requests
  9. import sys
  10. import json
  11. import dataset
  12. import random
  13. import os
  14. singles={}
  15. driver=None
  16. def click_btn(pathx):
  17. global driver
  18. elmts = driver.find_elements(By.XPATH, pathx)
  19. for elmt in elmts:
  20. webdriver.ActionChains(driver).move_to_element(elmt).perform()
  21. webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
  22. def init_webdriver(portnum):
  23. options = webdriver.ChromeOptions()
  24. # options.add_argument("--headless")
  25. options.add_argument("--disable-blink-features=AutomationControlled")
  26. options.add_argument('--ignore-certificate-errors')
  27. options.add_experimental_option("excludeSwitches", ["enable-automation"])
  28. options.add_experimental_option("useAutomationExtension", False)
  29. options.add_argument("--disable-gpu")
  30. options.add_argument("--disable-dev-shm-usage")
  31. # service = Service(executable_path="/usr/bin/chromedriver")
  32. # service = Service()
  33. # driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
  34. # driver = webdriver.Chrome(options, service)
  35. # driver = webdriver.Chrome(
  36. # options=options
  37. # )
  38. driver = webdriver.Remote(
  39. command_executor='http://127.0.0.1:'+str(portnum)+'/wd/hub',
  40. options=options)
  41. str1 = driver.capabilities['browserVersion']
  42. print('---')
  43. print(str1)
  44. print('---')
  45. # driver.set_window_size(1400,1000)
  46. # driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
  47. return driver
  48. r = requests.get('http://cmm.ai:3001/api/push/YEiFOTMrUY?status=up&msg=OK&ping=')
  49. qs=['台北101有多高','觀景台營業時間','請問101觀景台可以帶寵物嗎','台北101有置物櫃嗎','觀景台可以使用美金付款嗎','請問停車收費方式','最近有什麼優惠','有素食餐廳嗎','美食街怎麼走','台北101樓層介紹','台北101美食','台北101品牌','台北101世界第幾高2024?','台北101幾號出口?','蓋101死了多少人?','台北101觀景台需要預約嗎?']
  50. dockername='p4444'
  51. portnum=random.randint(4444,4555)
  52. print(portnum)
  53. os.system('docker container stop '+dockername)
  54. time.sleep(0.5)
  55. os.system('docker container rm '+dockername)
  56. time.sleep(0.5)
  57. os.system('docker run -d -p '+str(portnum)+':4444 --shm-size=2g --name '+dockername+' --dns 168.95.1.1 selenium/standalone-chrome:103.0')
  58. time.sleep(7)
  59. driver=init_webdriver(portnum)
  60. #driver.request_interceptor = interceptor
  61. driver.get('https://cmm.ai/101-aiv1')
  62. time.sleep(3)
  63. urls=[]
  64. click_btn("//button[contains(text(),'中文')]")
  65. time.sleep(3)
  66. click_btn("//button[@class='close-btn']")
  67. time.sleep(3)
  68. click_btn("//button[contains(text(),'點我問問題')]")
  69. time.sleep(3)
  70. i_key=random.choice(qs)
  71. elmts = driver.find_elements(By.XPATH, "//input[contains(@placeholder,'請輸入訊息')]")
  72. for elmt in elmts:
  73. elmt.send_keys(i_key)
  74. elmt.send_keys(Keys.ENTER)
  75. #中文
  76. time.sleep(10)
  77. print('----done----')