From 8a0b647b22c68a2fc3e502b9dd244803a539ab08 Mon Sep 17 00:00:00 2001 From: g1879 Date: Fri, 26 May 2023 18:37:00 +0800 Subject: [PATCH] =?UTF-8?q?page=E5=AF=B9=E8=B1=A1=E5=A2=9E=E5=8A=A0user=5F?= =?UTF-8?q?agent=E5=B1=9E=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_base.py | 5 +++++ DrissionPage/chromium_base.pyi | 3 +++ DrissionPage/chromium_tab.py | 8 ++++++++ DrissionPage/chromium_tab.pyi | 3 +++ DrissionPage/session_page.py | 5 +++++ DrissionPage/session_page.pyi | 3 +++ DrissionPage/web_page.py | 12 ++++++++++-- DrissionPage/web_page.pyi | 3 +++ 8 files changed, 40 insertions(+), 2 deletions(-) diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index bdb3429..afe5a2a 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -335,6 +335,11 @@ class ChromiumBase(BasePage): """返回页面加载策略,有3种:'none'、'normal'、'eager'""" return self._page_load_strategy + @property + def user_agent(self): + """返回user agent""" + return self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value'] + @property def scroll(self): """返回用于滚动滚动条的对象""" diff --git a/DrissionPage/chromium_base.pyi b/DrissionPage/chromium_base.pyi index 7961c06..d09771b 100644 --- a/DrissionPage/chromium_base.pyi +++ b/DrissionPage/chromium_base.pyi @@ -115,6 +115,9 @@ class ChromiumBase(BasePage): @property def page_load_strategy(self) -> str: ... + @property + def user_agent(self) -> str: ... + @property def scroll(self) -> ChromiumPageScroll: ... diff --git a/DrissionPage/chromium_tab.py b/DrissionPage/chromium_tab.py index 6a3b5e9..a5ef752 100644 --- a/DrissionPage/chromium_tab.py +++ b/DrissionPage/chromium_tab.py @@ -124,6 +124,14 @@ class WebPageTab(SessionPage, ChromiumTab): """以dict方式返回cookies""" return super().cookies + @property + def user_agent(self): + """返回user agent""" + if self._mode == 's': + return super().user_agent + elif self._mode == 'd': + return super(SessionPage, self).user_agent + @property def session(self): """返回Session对象,如未初始化则按配置信息创建""" diff --git a/DrissionPage/chromium_tab.pyi b/DrissionPage/chromium_tab.pyi index 835776f..a2dbe8a 100644 --- a/DrissionPage/chromium_tab.pyi +++ b/DrissionPage/chromium_tab.pyi @@ -67,6 +67,9 @@ class WebPageTab(SessionPage, ChromiumTab): @property def cookies(self) -> dict: ... + @property + def user_agent(self) -> str: ... + @property def session(self) -> Session: ... diff --git a/DrissionPage/session_page.py b/DrissionPage/session_page.py index d735313..20e1f04 100644 --- a/DrissionPage/session_page.py +++ b/DrissionPage/session_page.py @@ -98,6 +98,11 @@ class SessionPage(BasePage): except Exception: return None + @property + def user_agent(self): + """返回user agent""" + return self.session.headers.get('user-agent', '') + @property def download_path(self): """返回下载路径""" diff --git a/DrissionPage/session_page.pyi b/DrissionPage/session_page.pyi index 9251742..c13d488 100644 --- a/DrissionPage/session_page.pyi +++ b/DrissionPage/session_page.pyi @@ -60,6 +60,9 @@ class SessionPage(BasePage): @property def json(self) -> Union[dict, None]: ... + @property + def user_agent(self) -> str: ... + @property def download_path(self) -> str: ... diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index 9ab9269..dbdc5db 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -182,6 +182,14 @@ class WebPage(SessionPage, ChromiumPage, BasePage): """以dict方式返回cookies""" return super().cookies + @property + def user_agent(self): + """返回user agent""" + if self._mode == 's': + return super().user_agent + elif self._mode == 'd': + return super(SessionPage, self).user_agent + @property def session(self): """返回Session对象,如未初始化则按配置信息创建""" @@ -339,8 +347,8 @@ class WebPage(SessionPage, ChromiumPage, BasePage): return if copy_user_agent: - selenium_user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value'] - self.session.headers.update({"User-Agent": selenium_user_agent}) + user_agent = self.run_cdp('Runtime.evaluate', expression='navigator.userAgent;')['result']['value'] + self.session.headers.update({"User-Agent": user_agent}) set_session_cookies(self.session, self._get_driver_cookies()) diff --git a/DrissionPage/web_page.pyi b/DrissionPage/web_page.pyi index df5cbd3..cf46938 100644 --- a/DrissionPage/web_page.pyi +++ b/DrissionPage/web_page.pyi @@ -66,6 +66,9 @@ class WebPage(SessionPage, ChromiumPage, BasePage): @property def cookies(self) -> dict: ... + @property + def user_agent(self) -> str: ... + @property def session(self) -> Session: ...