优化_try_to_connect()

This commit is contained in:
g1879 2021-01-07 09:42:03 +08:00
parent b8a82d6bfc
commit 1649d8a3b6
2 changed files with 20 additions and 13 deletions

View File

@ -6,7 +6,6 @@
""" """
from glob import glob from glob import glob
from pathlib import Path from pathlib import Path
from time import time, sleep
from typing import Union, List, Any, Tuple from typing import Union, List, Any, Tuple
from urllib.parse import quote from urllib.parse import quote
@ -14,6 +13,7 @@ from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.chrome.webdriver import WebDriver from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support.wait import WebDriverWait
from time import time, sleep
from .common import str_to_loc, get_available_file_name, translate_loc, format_html from .common import str_to_loc, get_available_file_name, translate_loc, format_html
from .driver_element import DriverElement, execute_driver_find from .driver_element import DriverElement, execute_driver_find
@ -100,14 +100,23 @@ class DriverPage(object):
:param show_errmsg: 是否抛出异常 :param show_errmsg: 是否抛出异常
:return: 是否成功 :return: 是否成功
""" """
self.driver.get(to_url)
is_ok = self.check_page()
while times and is_ok is False: def goto() -> bool:
sleep(interval) try:
self.driver.get(to_url) self.driver.get(to_url)
is_ok = self.check_page() return True
times -= 1 except:
return False
is_ok = self.check_page() if goto() else False
for _ in range(times):
if is_ok is not False:
break
sleep(interval)
print(f'重试 {to_url}')
is_ok = self.check_page() if goto() else False
if is_ok is False and show_errmsg: if is_ok is False and show_errmsg:
raise ConnectionError('Connect error.') raise ConnectionError('Connect error.')

View File

@ -10,11 +10,11 @@ from pathlib import Path
from random import randint from random import randint
from re import search as re_SEARCH from re import search as re_SEARCH
from re import sub as re_SUB from re import sub as re_SUB
from time import time, sleep
from typing import Union, List, Tuple from typing import Union, List, Tuple
from urllib.parse import urlparse, quote, unquote from urllib.parse import urlparse, quote, unquote
from requests import Session, Response from requests import Session, Response
from time import time, sleep
from tldextract import extract from tldextract import extract
from .common import str_to_loc, translate_loc, get_available_file_name, format_html from .common import str_to_loc, translate_loc, get_available_file_name, format_html
@ -199,15 +199,13 @@ class SessionPage(object):
""" """
r = self._make_response(to_url, mode=mode, show_errmsg=show_errmsg, **kwargs)[0] r = self._make_response(to_url, mode=mode, show_errmsg=show_errmsg, **kwargs)[0]
while times and (not r or r.content == b''): for _ in range(times):
if r is not None and r.status_code in (403, 404): if (r and r.content != b'') or (r is not None and r.status_code in (403, 404)):
break break
print('重试', to_url) print(f'重试 {to_url}')
sleep(interval) sleep(interval)
r = self._make_response(to_url, mode=mode, show_errmsg=show_errmsg, **kwargs)[0] r = self._make_response(to_url, mode=mode, show_errmsg=show_errmsg, **kwargs)[0]
times -= 1
return r return r