import traceback from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.chrome.service import Service import json import redis import random import os import time import redis driver = None def re_get_webdriver(): global port global driver result = [] if driver is not None: print('closing....') driver.quit() os.system('killall chrome') print('quit....') driver = None try: s = Service('/root/driver/chromedriver102') #s = Service('/root/driver/chromedriver') options = webdriver.ChromeOptions() options.add_argument("--no-sandbox") options.add_argument("--disable-dev-shm-usage") options.add_argument("--headless") #options.add_argument('--remote-debugging-port=9222') #options.add_experimental_option("debuggerAddress", '127.0.0.1:9927') options.add_argument("--incognito") r = redis.Redis(host='db.ptt.cx', port=6379, db=2, password='choozmo9') data = r.get('google_proxy') jstext = data.decode('utf-8') jsobj = json.loads(jstext) proxy = random.choice(jsobj) change_ip_list = ['--proxy-server=%s' % proxy, "--proxy-server=socks5://127.0.0.1:9050", "--proxy-server=socks5://192.53.174.202:8180"] change_ip = random.choice(change_ip_list) #options.add_argument('--proxy-server=socks5://172.104.93.163:41800') print('使用代理ip', change_ip) #driver.delete_all_cookies() driver = webdriver.Chrome(options=options) driver.set_window_size(1400, 1000) except: traceback.print_exc() driver = None return None def run_once(): global driver re_get_webdriver() driver.get('https://api.ipify.org/') time.sleep(3) print(driver.current_url) ip_address = driver.find_element(By.TAG_NAME, "body").text print(ip_address) driver.quit() #os.system('docker container restart tiny6') time.sleep(1) run_once()