local_1777.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. import dataset
  13. import json
  14. import random
  15. import time
  16. import sys
  17. import codecs
  18. import random
  19. import os
  20. import time
  21. driver=None
  22. def re_get_webdriver():
  23. global driver
  24. result=[]
  25. # client = docker.from_env()
  26. # ls=client.containers.list()
  27. # print(ls)
  28. # for l in ls:
  29. # if 'p17777' in l.name:
  30. # ls[0].restart()
  31. # time.sleep(4)
  32. if driver is not None:
  33. print('closing....')
  34. driver.quit()
  35. # options = webdriver.EdgeOptions()
  36. try:
  37. options = webdriver.ChromeOptions()
  38. options.add_argument("--no-sandbox")
  39. options.add_argument("--disable-dev-shm-usage")
  40. options.add_argument("--headless")
  41. driver = webdriver.Chrome(options=options)
  42. # desired_capabilities=options.to_capabilities())
  43. driver.set_window_size(1400,1000)
  44. return
  45. except:
  46. import traceback
  47. traceback.print_exc()
  48. driver=None
  49. return None
  50. driver=None
  51. def run_once(url):
  52. global driver
  53. i=random.randint(0,15)
  54. if i==0 or driver is None:
  55. # if True:
  56. re_get_webdriver()
  57. time.sleep(3)
  58. if driver is None:
  59. return
  60. try:
  61. driver.execute_script('window.open("'+url+'","_blank");')
  62. driver.execute_script("window.scrollTo(0, window.scrollY + 400)")
  63. time.sleep(0.5)
  64. except:
  65. print('exception')
  66. lst=[]
  67. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  68. cursor=db.query('SELECT * FROM columnids order by rand()')
  69. for c in cursor:
  70. lst.append('https://www.hhh.com.tw/columns/detail/'+str(c['cid'])+'/index.php')
  71. #lst=['https://www.hhh.com.tw/columns/detail/3427/index.php']
  72. #for i in range(20):
  73. while True:
  74. l=random.choice(lst)
  75. print(l)
  76. run_once(l)