|
14 | 14 | from selenium.common.exceptions import WebDriverException, UnexpectedAlertPresentException |
15 | 15 | from selenium.webdriver.firefox.options import Options as FirefoxOptions |
16 | 16 | from urllib.parse import urlparse |
17 | | -from fake_useragent import UserAgent |
18 | 17 | from contextlib import contextmanager |
19 | 18 | from deprecated import deprecated |
20 | 19 | try: |
|
24 | 23 | except ImportError: |
25 | 24 | stem = None |
26 | 25 |
|
| 26 | +try: |
| 27 | + from fake_useragent import UserAgent |
| 28 | + FAKE_USERAGENT = True |
| 29 | +except Exception: |
| 30 | + FAKE_USERAGENT = False |
| 31 | + DEFAULT_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36' |
| 32 | + |
27 | 33 | from .data_types import ProxyMode |
28 | 34 |
|
29 | 35 |
|
@@ -454,13 +460,18 @@ def _new_session(self, **kwargs): |
454 | 460 | # self._session = httpx.Client() |
455 | 461 | self.got_403 = False |
456 | 462 |
|
457 | | - # Suppress the misleading traceback from UserAgent() |
458 | | - with self._suppress_logger('fake_useragent'): |
459 | | - _HEADERS = { |
460 | | - 'accept-language': 'en-US,en', |
461 | | - 'accept': 'text/html,application/xhtml+xml,application/xml', |
462 | | - 'User-Agent': UserAgent().random, |
463 | | - } |
| 463 | + if FAKE_USERAGENT: |
| 464 | + # Suppress the misleading traceback from UserAgent() |
| 465 | + with self._suppress_logger('fake_useragent'): |
| 466 | + user_agent = UserAgent().random |
| 467 | + else: |
| 468 | + user_agent = DEFAULT_USER_AGENT |
| 469 | + |
| 470 | + _HEADERS = { |
| 471 | + 'accept-language': 'en-US,en', |
| 472 | + 'accept': 'text/html,application/xhtml+xml,application/xml', |
| 473 | + 'User-Agent': user_agent, |
| 474 | + } |
464 | 475 | # self._session.headers.update(_HEADERS) |
465 | 476 | init_kwargs.update(headers=_HEADERS) |
466 | 477 |
|
|
0 commit comments