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

 

posted @ 2020-12-06 17:37  Mr-刘  阅读(230)  评论(0编辑  收藏  举报