def _is_reachable(self, url, count) -> bool: """ 判断网站是否可访问 1. 超时->不可访问 2. 含有`error-code`->不可访问 :param url: :param count: :return: """ LOG.debug( "[Worker]Worker: {} REQUESTING the url: {}, life: {}/{}.".format( self.tid, url, count, RETRIES)) try: url = pad_url(url) self.driver.get(url) try: # 1. 含有error-code self.driver.find_element_by_xpath('//*[@class="error-cod1e"]') return False except NoSuchElementException: pass # 可能有Alert弹窗 # TODO 其他弹窗 try: self.driver.switch_to.alert.accept() except NoAlertPresentException: pass return True except TimeoutException: # 2. 超时 return False
def _is_reachable(self, url, count, cookies={}) -> bool: """ 判断网站是否可访问 1. 超时->不可访问 2. 含有`error-code`->不可访问 :param url: :param count: :return: """ LOG.debug("[TM-Worker]Worker: {} REQUESTING the url: {}, life: {}/{}.". format(self.tid, url, count, RETRIES)) try: url = pad_url(url) self.driver.delete_all_cookies() if isinstance(cookies, str) and cookies: cookies_names_values_domains = get_cookies_names_values_domains( url, cookies) LOG.debug( "[TM-Worker]Worker {} cookies_names_values_domains: {}". format(self.tid, cookies_names_values_domains)) for nv in cookies_names_values_domains: self.driver.add_cookie(nv) self.driver.get(url) try: # 1. 含有error-code self.driver.find_element_by_xpath('//*[@class="error-cod1e"]') return False except NoSuchElementException: pass # 可能有Alert弹窗 # TODO 其他弹窗 try: self.driver.switch_to.alert.accept() except NoAlertPresentException: pass return True except TimeoutException: # 2. 超时 return False
def _is_reachable(self, url, count) -> bool: """ 判断网站是否可访问 是否在规定的时间内加载完 全页面元素, 是则为可访问,不是则为不可访问 :param url: :param count: :return: """ LOG.debug( "[Worker]Worker: {} REQUESTING the url: {}, life: {}.".format( self.tid, url, count)) try: url = pad_url(url) self.driver.get(url) # 可能有Alert弹窗 # TODO 其他弹窗 try: self.driver.switch_to.alert.accept() except NoAlertPresentException: pass return True except TimeoutException: return False
def _is_reachable(self, url, count, cookies={}) -> bool: """ 判断网站是否可访问 是否在规定的时间内加载完 全页面元素, 是则为可访问,不是则为不可访问 :param url: :param count: :return: """ LOG.debug( "[Worker]Worker: {} REQUESTING the url: {}, life: {}.".format( self.tid, url, count)) try: url = pad_url(url) self.driver.delete_all_cookies() if isinstance(cookies, str) and cookies: cookies_names_values_domains = get_cookies_names_values_domains( url, cookies) LOG.debug("[Worker]Worker {} cookies_names_values_domains: {}". format(self.tid, cookies_names_values_domains)) for nv in cookies_names_values_domains: self.driver.add_cookie(nv) self.driver.get(url) # 可能有Alert弹窗 # TODO 其他弹窗 try: self.driver.switch_to.alert.accept() except NoAlertPresentException: pass return True except TimeoutException: return False except Exception as e: LOG.debug( "[Worker]Worker {} cannot reach URL: {} Cookies: {} error: {}". format(self.tid, url, cookies, str(e))) return False