local_1777.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. mobile_emulation = {
  37. "deviceMetrics": { "width": 360, "height": 640, "pixelRatio": 3.0 },
  38. "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19" }
  39. options.add_experimental_option("mobileEmulation", mobile_emulation)
  40. options.add_argument("--no-sandbox")
  41. options.add_argument("--disable-dev-shm-usage")
  42. # options.add_argument("--headless")
  43. # print(user_agent)
  44. # options.add_argument("--user-agent=" +user_agent)
  45. options.add_argument("--incognito")
  46. driver=None
  47. try:
  48. driver = webdriver.Chrome(options=options)
  49. except:
  50. # driver.quit()
  51. # os.system('pkill -f ')
  52. os.system('kill %d' % os.getpid())
  53. sys.exit()
  54. return
  55. # driver.set_window_size(1400,1000)
  56. return
  57. except:
  58. import traceback
  59. traceback.print_exc()
  60. driver=None
  61. return None
  62. def run_once(url):
  63. global driver
  64. i=random.randint(0,7)
  65. if i==0 or driver is None:
  66. time.sleep(8)
  67. re_get_webdriver()
  68. if driver is None:
  69. return
  70. try:
  71. driver.execute_script('window.open("'+url+'","_blank");')
  72. driver.execute_script("window.scrollTo(0, window.scrollY + 400)")
  73. time.sleep(0.5)
  74. except:
  75. print('exception')
  76. lst=[]
  77. #db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4')
  78. db = dataset.connect('mysql://choozmo:pAssw0rd@172.105.194.225:3306/seo?charset=utf8mb4')
  79. cursor=db.query('SELECT * FROM columnids order by rand()')
  80. for c in cursor:
  81. # lst.append('https://www.hhh.com.tw/columns/detail/'+str(c['cid'])+'/')
  82. lst.append('https://m.hhh.com.tw/columns/detail/'+str(c['cid'])+'/')
  83. #lst=['https://www.hhh.com.tw/columns/detail/3427/index.php']
  84. #for i in range(20):
  85. while True:
  86. l=random.choice(lst)
  87. print(l)
  88. run_once(l)