diff --git a/DrissionPage/_base/chromium_driver.py b/DrissionPage/_base/chromium_driver.py index 83309a4..b9f1c9e 100644 --- a/DrissionPage/_base/chromium_driver.py +++ b/DrissionPage/_base/chromium_driver.py @@ -71,26 +71,29 @@ class ChromiumDriver(object): try: self._ws.send(message_json) except OSError: - self.method_results.pop(ws_id) + self.method_results.pop(ws_id, None) return None while not self._stopped.is_set(): try: - return self.method_results[ws_id].get(timeout=.2) + result = self.method_results[ws_id].get(timeout=.2) + self.method_results.pop(ws_id, None) + return result except Empty: if self.alert_flag: self.alert_flag = False - return {'result': {'message': 'alert exists.'}} + result = {'result': {'message': 'alert exists.'}} + self.method_results.pop(ws_id, None) + return result elif timeout is not None and perf_counter() > timeout: - return {'error': {'message': 'timeout'}} + result = {'error': {'message': 'timeout'}} + self.method_results.pop(ws_id, None) + return result continue - finally: - self.method_results.pop(ws_id) - def _recv_loop(self): """接收浏览器信息的守护线程方法""" while not self._stopped.is_set(): diff --git a/DrissionPage/_commons/web.py b/DrissionPage/_commons/web.py index 543d222..9d9c7b5 100644 --- a/DrissionPage/_commons/web.py +++ b/DrissionPage/_commons/web.py @@ -169,8 +169,8 @@ def cookie_to_dict(cookie): """ if isinstance(cookie, Cookie): cookie_dict = cookie.__dict__.copy() - cookie_dict.pop('rfc2109') - cookie_dict.pop('_rest') + cookie_dict.pop('rfc2109', None) + cookie_dict.pop('_rest', None) return cookie_dict elif isinstance(cookie, dict): diff --git a/DrissionPage/_configs/chromium_options.py b/DrissionPage/_configs/chromium_options.py index 1ce072f..214ccd0 100644 --- a/DrissionPage/_configs/chromium_options.py +++ b/DrissionPage/_configs/chromium_options.py @@ -209,7 +209,7 @@ class ChromiumOptions(object): :param arg: 设置项名称 :return: 当前对象 """ - self._prefs.pop(arg) + self._prefs.pop(arg, None) return self def remove_pref_from_file(self, arg): diff --git a/DrissionPage/_configs/session_options.py b/DrissionPage/_configs/session_options.py index 4339b08..d918f63 100644 --- a/DrissionPage/_configs/session_options.py +++ b/DrissionPage/_configs/session_options.py @@ -161,8 +161,7 @@ class SessionOptions(object): return self attr = attr.lower() - if attr in self._headers: - self._headers.pop(attr) + self._headers.pop(attr, None) return self diff --git a/DrissionPage/_units/download_manager.py b/DrissionPage/_units/download_manager.py index 0fbd75f..a36d362 100644 --- a/DrissionPage/_units/download_manager.py +++ b/DrissionPage/_units/download_manager.py @@ -101,7 +101,7 @@ class BrowserDownloadManager(object): mission.final_path = final_path if mission.tab_id in self._tab_missions and mission.id in self._tab_missions[mission.tab_id]: self._tab_missions[mission.tab_id].remove(mission.id) - self._missions.pop(mission.id) + self._missions.pop(mission.id, None) mission._is_done = True def cancel(self, mission): @@ -127,18 +127,9 @@ class BrowserDownloadManager(object): :param tab_id: 标签页id :return: None """ - try: - self._tab_missions.pop(tab_id) - except KeyError: - pass - try: - self._flags.pop(tab_id) - except KeyError: - pass - try: - TabDownloadSettings.TABS.pop(tab_id) - except KeyError: - pass + self._tab_missions.pop(tab_id, None) + self._flags.pop(tab_id, None) + TabDownloadSettings.TABS.pop(tab_id, None) def _onDownloadWillBegin(self, **kwargs): """用于获取弹出新标签页触发的下载任务""" diff --git a/DrissionPage/_units/network_listener.py b/DrissionPage/_units/network_listener.py index 9732330..2b2214a 100644 --- a/DrissionPage/_units/network_listener.py +++ b/DrissionPage/_units/network_listener.py @@ -72,7 +72,7 @@ class NetworkListener(object): :param method: 设置监听的请求类型,可用list等指定多个,为None时监听全部 :return: None """ - if targets: + if targets or method: self.set_targets(targets, is_regex, method) if self.listening: return @@ -191,23 +191,26 @@ class NetworkListener(object): def _requestWillBeSent(self, **kwargs): """接收到请求时的回调函数""" if not self._targets: - rid = kwargs['requestId'] - p = self._request_ids.setdefault(rid, DataPacket(self._page.tab_id, None)) - p._raw_request = kwargs - if kwargs['request'].get('hasPostData', None) and not kwargs['request'].get('postData', None): - p._raw_post_data = self._driver.call_method('Network.getRequestPostData', requestId=rid)['postData'] - return - - rid = kwargs['requestId'] - for target in self._targets: - if ((self._is_regex and search(target, kwargs['request']['url'])) or - (not self._is_regex and target in kwargs['request']['url'])) and ( - not self._method or kwargs['request']['method'] in self._method): - p = self._request_ids.setdefault(rid, DataPacket(self._page.tab_id, target)) + if not self._method or kwargs['request']['method'] in self._method: + rid = kwargs['requestId'] + p = self._request_ids.setdefault(rid, DataPacket(self._page.tab_id, None)) p._raw_request = kwargs if kwargs['request'].get('hasPostData', None) and not kwargs['request'].get('postData', None): p._raw_post_data = self._driver.call_method('Network.getRequestPostData', requestId=rid)['postData'] - break + return + + else: + rid = kwargs['requestId'] + for target in self._targets: + if ((self._is_regex and search(target, kwargs['request']['url'])) or + (not self._is_regex and target in kwargs['request']['url'])) and ( + not self._method or kwargs['request']['method'] in self._method): + p = self._request_ids.setdefault(rid, DataPacket(self._page.tab_id, target)) + p._raw_request = kwargs + if kwargs['request'].get('hasPostData', None) and not kwargs['request'].get('postData', None): + p._raw_post_data = self._driver.call_method('Network.getRequestPostData', requestId=rid)[ + 'postData'] + break def _requestWillBeSentExtraInfo(self, **kwargs): self._extra_info_ids.setdefault(kwargs['requestId'], {})['request'] = kwargs @@ -244,11 +247,8 @@ class NetworkListener(object): self._caught.put(dp) - try: - self._request_ids.pop(r_id) - self._extra_info_ids.pop(r_id) - except: - pass + self._request_ids.pop(r_id, None) + self._extra_info_ids.pop(r_id, None) def _loading_failed(self, **kwargs): """请求失败时的回调方法""" @@ -263,11 +263,8 @@ class NetworkListener(object): dp._responseExtraInfo = ei.get('response', None) self._caught.put(dp) - try: - self._request_ids.pop(r_id) - self._extra_info_ids.pop(r_id) - except: - pass + self._request_ids.pop(r_id, None) + self._extra_info_ids.pop(r_id, None) class DataPacket(object):