记一次selenium + Chrom 无法捕获的异常 报错 selenium.common.exceptions.TimeoutException: Message: timeout

Traceback (most recent call last):
  File "C:/Users/Administrator/Desktop/WenShuSpider_v1/DetailCrawl/ChromeProxyDetail_4.py", line 86, in get_detail
    self.driver.get(url)
  File "C:\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout
  (Session info: chrome=73.0.3683.103)
  (Driver info: chromedriver=70.0.3538.97 (d035916fe243477005bc95fe2a5778b8f20b6ae1),platform=Windows NT 10.0.17134 x86_64)

抓取某网站的时候,由于网站经常会响应比较慢,或者还没加载完全就返回空白页,导致程序经常检测不到元素卡死,返回

selenium.common.exceptions.TimeoutException: Message: timeout

的报错。

通过在 try ... except 后面添加 finally: 当try和except没有完成时让页面刷新程序重新调用(刷新页面或许有用)

    def get_detail(self, url):
        msgtimeout = 1
        try:
            print('访问网址:{}'.format(url))
            self.driver.get(url)

            myxpath = '//div[@class="PDF_pox"]//*[text()]'
            locator = (By.XPATH, myxpath)
            self.wait.until(EC.presence_of_element_located(locator))
            time.sleep(0.25)
            html = self.driver.page_source
            msgtimeout = 0
            return html
        except Exception as e:
            print('获取详情页失败:{}'.format(e))
            # 清除 cookie
            self.driver.delete_all_cookies()
            print('清除cookie 重新访问 {}'.format(url))
            msgtimeout = 0
            return self.get_detail(url)
        finally:
            if msgtimeout:
                print('wo 来处理timeout异常')
                return self.get_detail(url)

 

  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值