local_1777.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. from userAgentRandomizer import userAgents
  22. driver=None
  23. def re_get_webdriver():
  24. global driver
  25. result=[]
  26. if driver is not None:
  27. print('closing....')
  28. driver.quit()
  29. os.system('killall chrome')
  30. print('quit....')
  31. driver=None
  32. try:
  33. ua = userAgents()
  34. user_agent = ua.random()
  35. options = webdriver.ChromeOptions()
  36. options.add_argument("--no-sandbox")
  37. options.add_argument("--disable-dev-shm-usage")
  38. options.add_argument("--headless")
  39. print(user_agent)
  40. options.add_argument("--user-agent=" +user_agent)
  41. options.add_argument("--incognito")
  42. driver=None
  43. try:
  44. driver = webdriver.Chrome(options=options)
  45. except:
  46. traceback.print_exc()
  47. # driver.quit()
  48. # os.system('pkill -f ')
  49. os.system('kill %d' % os.getpid())
  50. sys.exit()
  51. return
  52. driver.set_window_size(1400,1000)
  53. return
  54. except:
  55. import traceback
  56. traceback.print_exc()
  57. driver=None
  58. return None
  59. def run_once(url):
  60. global driver
  61. i=random.randint(0,7)
  62. if i==0 or driver is None:
  63. time.sleep(8)
  64. re_get_webdriver()
  65. if driver is None:
  66. return
  67. try:
  68. driver.execute_script('window.open("'+url+'","_blank");')
  69. driver.execute_script("window.scrollTo(0, window.scrollY + 400)")
  70. time.sleep(0.5)
  71. except:
  72. print('exception')
  73. lst=[]
  74. lst.append('https://innews.com.tw/62183/')
  75. lst.append('https://innews.com.tw/48338/')
  76. lst.append('https://innews.com.tw/62326/')
  77. lst.append('https://innews.com.tw/38246/')
  78. lst.append('https://innews.com.tw/24843/')
  79. #lst=['https://www.hhh.com.tw/columns/detail/3427/index.php']
  80. #for i in range(20):
  81. #while True:
  82. for i in range(500):
  83. l=random.choice(lst)
  84. print(l)
  85. run_once(l)