#import redis import time import traceback #import json from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities import time import os from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC import dataset import json import random import time import sys import codecs import random import os import time from userAgentRandomizer import userAgents driver=None def re_get_webdriver(): global driver result=[] if driver is not None: print('closing....') driver.quit() os.system('killall chrome') print('quit....') driver=None try: ua = userAgents() user_agent = ua.random() options = webdriver.ChromeOptions() options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--headless") print(user_agent) options.add_argument("--user-agent=" +user_agent) options.add_argument("--incognito") driver = webdriver.Chrome(options=options) driver.set_window_size(1400,1000) return except: import traceback traceback.print_exc() driver=None return None def run_once(url): global driver i=random.randint(0,7) if i==0 or driver is None: time.sleep(8) re_get_webdriver() if driver is None: return try: driver.execute_script('window.open("'+url+'","_blank");') driver.execute_script("window.scrollTo(0, window.scrollY + 400)") time.sleep(0.5) except: print('exception') lst=[] db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/seo?charset=utf8mb4') cursor=db.query('SELECT * FROM columnids order by rand()') for c in cursor: lst.append('https://www.hhh.com.tw/columns/detail/'+str(c['cid'])+'/') #lst=['https://www.hhh.com.tw/columns/detail/3427/index.php'] #for i in range(20): while True: l=random.choice(lst) print(l) run_once(l)