def stop(self, proto_timeout=10): """控制指定的tab页停止浏览.返回值:错误消息,空正常""" try: rst = self.call_method('Page.stopLoading', _timeout=proto_timeout) return '' except Exception as e: return None, spd_base.es(e)
def _handle_js_dialog(enable=False, proto_timeout=10): """在对话框事件回调中进行调用,禁用或启用页面上的js对话框""" try: rst = self.call_method('Page.handleJavaScriptDialog', accept=enable, _timeout=proto_timeout) return True, '' except Exception as e: return False, spd_base.es(e)
def goto(self, url, proto_timeout=10): """控制tab页浏览指定的url.返回值({'frameId': 主框架id, 'loaderId': 装载器id}, 错误消息)""" try: self.clear_request_historys() # 每次发起新导航的时候,都清空之前记录的请求信息 self.last_url = url rst = self.call_method('Page.navigate', url=url, _timeout=proto_timeout) return rst, '' except Exception as e: return None, spd_base.es(e)
def _recv(self, timeout=0.01): """尝试进行一次接收处理. 返回值:(结果数,事件数,错误消息) (0,0,'')超时; (None,None,err)通信错误 """ if not self._websocket: return (None, None, 'not websocket connection.') try: self._websocket.settimeout(timeout) message_json = self._websocket.recv() message = json.loads(message_json) # 接收到json消息后就转换为对象 except websocket.WebSocketTimeoutException: return (0, 0, '') # 超时了,什么都没有收到 except websocket.WebSocketException as e: return (None, None, spd_base.es(e)) # websocket错误 except Exception as e: return (None, None, spd_base.es(e)) # 其他错误 if self.debug: # 如果开启了调试输出,则打印接收到的消息 print('< RECV %s' % message_json) if "method" in message: # 接收到事件报文,尝试进行回调处理 method = message['method'] if method in self.event_handlers: try: self.event_handlers[method](**message['params']) except Exception as e: logger.warning("callback %s exception %s" % (method, py_util.get_trace_stack())) return (0, 1, '') elif "id" in message: # 接收到结果报文 msg_id = message["id"] if msg_id in self.method_results: self.method_results[msg_id] = message # 得到了等待的对应结果,则记录下来 return (1, 0, '') else: logger.warning("unknown CDP message: %s" % (message)) return (None, None, 'unknown CDP message.') return (0, 0, '')
def get_response_body(self, reqid, proto_timeout=10): """根据请求id获取回应body内容.需要先确保回应已经完成,否则出错. 返回值:(body,err) err为空则正常 """ try: rst = self.call_method('Network.getResponseBody', requestId=reqid, _timeout=proto_timeout) if rst is None: return None, 'Network.getResponseBody call fail: %s' % self.last_err body = rst['body'] en = rst['base64Encoded'] if en: body = base64.decodebytes(body.encode('latin-1')) return body, '' except Exception as e: return None, spd_base.es(e)
def get_request_infos(self, url, url_is_re=False): """获取指定url的请求信息 工作流程:1 打开tab页的时候,就需要告知url的匹配模式;2 等待页面装载完成,内部记录发送的请求信息; 3根据url查找发送的请求内容. 返回值: [(request,requestId)],msg msg为''则正常;request为请求内容;requestId为请求ID,可据此获取更多数据. """ try: if url_is_re: urls = self.get_request_urls(url, url_is_re) if len(urls) == 0: return None, '' rst = [] for u in urls: rst.extend(self.get_request_info(u)) return rst, '' else: return self.get_request_info(url), '' except Exception as e: return None, spd_base.es(e)