zoukankan      html  css  js  c++  java
  • 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("
    ")
                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
  • 相关阅读:
    Kafka概述与部署
    理解微前端技术原理
    Docker之单机网络一
    MySQL Binlog解析
    MySQL之Xtrabackup
    MySQL之mysqldump
    学生成绩管理系统
    凯域课程表使用体验(软件名:上课了)
    动手动脑问题分析
    关于十天集训的收获
  • 原文地址:https://www.cnblogs.com/xinzaiyuan/p/14093444.html
Copyright © 2011-2022 走看看