mirror of
https://gitee.com/g1879/DrissionPage.git
synced 2024-12-10 04:00:23 +08:00
优化_try_to_connect()
This commit is contained in:
parent
b8a82d6bfc
commit
1649d8a3b6
@ -6,7 +6,6 @@
|
||||
"""
|
||||
from glob import glob
|
||||
from pathlib import Path
|
||||
from time import time, sleep
|
||||
from typing import Union, List, Any, Tuple
|
||||
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.remote.webelement import WebElement
|
||||
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 .driver_element import DriverElement, execute_driver_find
|
||||
@ -100,14 +100,23 @@ class DriverPage(object):
|
||||
:param show_errmsg: 是否抛出异常
|
||||
:return: 是否成功
|
||||
"""
|
||||
self.driver.get(to_url)
|
||||
is_ok = self.check_page()
|
||||
|
||||
while times and is_ok is False:
|
||||
def goto() -> bool:
|
||||
try:
|
||||
self.driver.get(to_url)
|
||||
return True
|
||||
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)
|
||||
self.driver.get(to_url)
|
||||
is_ok = self.check_page()
|
||||
times -= 1
|
||||
print(f'重试 {to_url}')
|
||||
is_ok = self.check_page() if goto() else False
|
||||
|
||||
if is_ok is False and show_errmsg:
|
||||
raise ConnectionError('Connect error.')
|
||||
|
@ -10,11 +10,11 @@ from pathlib import Path
|
||||
from random import randint
|
||||
from re import search as re_SEARCH
|
||||
from re import sub as re_SUB
|
||||
from time import time, sleep
|
||||
from typing import Union, List, Tuple
|
||||
from urllib.parse import urlparse, quote, unquote
|
||||
|
||||
from requests import Session, Response
|
||||
from time import time, sleep
|
||||
from tldextract import extract
|
||||
|
||||
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]
|
||||
|
||||
while times and (not r or r.content == b''):
|
||||
if r is not None and r.status_code in (403, 404):
|
||||
for _ in range(times):
|
||||
if (r and r.content != b'') or (r is not None and r.status_code in (403, 404)):
|
||||
break
|
||||
|
||||
print('重试', to_url)
|
||||
print(f'重试 {to_url}')
|
||||
sleep(interval)
|
||||
|
||||
r = self._make_response(to_url, mode=mode, show_errmsg=show_errmsg, **kwargs)[0]
|
||||
times -= 1
|
||||
|
||||
return r
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user