compact_chrome.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #import redis
  2. import time
  3. import traceback
  4. #import json
  5. from selenium import webdriver
  6. from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
  7. import time
  8. import os
  9. from selenium.webdriver.support.ui import WebDriverWait
  10. from selenium.webdriver.common.by import By
  11. from selenium.webdriver.support import expected_conditions as EC
  12. from selenium.webdriver.common.action_chains import ActionChains
  13. import dataset
  14. import random
  15. from selenium.webdriver.common.keys import Keys
  16. lst=[]
  17. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  18. os.system("docker container restart headless-shell")
  19. time.sleep(6)
  20. cursor=db.query('SELECT * FROM columnids order by rand()')
  21. for c in cursor:
  22. lst.append('https://www.hhh.com.tw/columns/detail/'+str(c['cid'])+'/')
  23. chrome_options = webdriver.ChromeOptions()
  24. chrome_options.add_argument("--incognito")
  25. chrome_options.add_argument("--headless")
  26. chrome_options.add_argument("--no-sandbox")
  27. chrome_options.add_argument("--disable-dev-shm-usage")
  28. chrome_options.add_argument("start-maximized")
  29. chrome_options.add_argument("user-data-dir=/tmp");
  30. chrome_options.debugger_address="127.0.0.1:9222"
  31. chrome98=r'C:\portable\webdriver\chrome98\chromedriver.exe'
  32. driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=chrome98)
  33. driver.get('https://tw.yahoo.com/')
  34. time.sleep(5)
  35. #while True:
  36. for i in range(10):
  37. url=random.choice(lst)
  38. print(url)
  39. driver.get(url)
  40. # time.sleep(8)
  41. # driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
  42. # ActionChains(driver).key_down(Keys.CONTROL).send_keys('t').key_up(Keys.CONTROL).perform()
  43. # time.sleep(2)
  44. driver.execute_script('window.open("'+url+'","_blank");')
  45. driver.execute_script("window.scrollTo(0, window.scrollY + 400)")
  46. time.sleep(8)
  47. # time.sleep(9999)
  48. #driver.save_screenshot('c:/tmp/sc.png')