1. 程式人生 > 實用技巧 >selenium設定程式失敗後重啟

selenium設定程式失敗後重啟

1. 程式出現異常丟擲,將異常寫入檔案,繼續執行下一條

    for key, value in I:
        # print(value)
        # 定義臨時變數為flase
        flag = False
        browser = None
        try:
            chrome_opt = webdriver.ChromeOptions()
            chrome_opt.add_argument(
                'user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"
') chrome_opt.add_argument('window-size=1200x1050') chrome_opt.add_experimental_option('excludeSwitches', ['enable-automation']) chrome_opt.add_argument('blink-settings=imagesEnabled=false') chrome_opt.add_argument('--disable-infobars') chrome_opt.add_argument(
"--headless") chrome_opt.add_argument('--disable-gpu') chrome_opt.add_argument('--no-sandbox') chrome_opt.add_argument("--proxy-server=%s" % func1(ip_list)) browser = webdriver.Chrome( executable_path="./chromedriver.exe", options
=chrome_opt) # driver = webdriver.Chrome(options=chrome_opt) # browser.implicitly_wait(20) browser.get("http://diaocha.iphouse.com.cn/s/aANnea/") # time.sleep(1) re.search(r'下一頁(.*)', browser.page_source).group() flag = True except Exception as e: # with open("error.txt", "a+", encoding="utf-8") as fp: # fp.write(json.dumps(value,ensure_ascii=False)) # fp.write("\n") print(e) for i in range(3): try: chrome_opt = webdriver.ChromeOptions() chrome_opt.add_argument( 'user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36"', ) chrome_opt.add_argument('window-size=1200x1050') chrome_opt.add_experimental_option('excludeSwitches', ['enable-automation']) chrome_opt.add_argument('blink-settings=imagesEnabled=false') chrome_opt.add_argument('--disable-infobars') chrome_opt.add_argument("--headless") chrome_opt.add_argument('--disable-gpu') chrome_opt.add_argument('--no-sandbox') chrome_opt.add_argument("--proxy-server=%s" % func1(ip_list)) browser = webdriver.Chrome( executable_path="./chromedriver.exe", options=chrome_opt) # driver = webdriver.Chrome(options=chrome_opt) # browser.implicitly_wait(20) browser.get("http://diaocha.iphouse.com.cn/s/aANnea/") # time.sleep(1) re.search(r'下一頁(.*)', browser.page_source).group() except Exception as e: print("desc_", e) if browser and browser.page_source and re.search(r'下一頁(.*)', browser.page_source): flag = True break if flag: # 正常任務執行狀態
Code