From 2696229a02b5f3063505f2c780f3f974f709500e Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 10 Aug 2020 18:50:28 +0800 Subject: [PATCH] =?UTF-8?q?s=E6=A8=A1=E5=BC=8F=E4=B8=8B=E5=85=83=E7=B4=A0?= =?UTF-8?q?=E5=8F=AF=E7=94=A8xpath=E6=89=BE=E5=88=B0=E4=B8=8A=E7=BA=A7?= =?UTF-8?q?=E6=88=96=E5=90=8C=E7=BA=A7=E5=85=83=E7=B4=A0=EF=BC=8C=E4=BB=A5?= =?UTF-8?q?=E5=89=8D=E5=8F=AA=E8=83=BD=E6=89=BE=E4=B8=8B=E7=BA=A7=EF=BC=8C?= =?UTF-8?q?=E5=BE=85=E5=A4=A7=E9=87=8F=E6=B5=8B=E8=AF=95=EF=BC=8C=E5=BE=85?= =?UTF-8?q?=E7=A0=94=E7=A9=B6css=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/session_element.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/DrissionPage/session_element.py b/DrissionPage/session_element.py index 1fe8ee6..e84d642 100644 --- a/DrissionPage/session_element.py +++ b/DrissionPage/session_element.py @@ -135,7 +135,7 @@ class SessionElement(DrissionElement): loc_str = None if loc_or_str[0] == 'xpath': # Element的html是包含自己的,要如下处理,使其只检索下级的 - loc_str = f'./{self.tag}{loc_or_str[1].lstrip(".")}' + loc_str = loc_or_str[1] if loc_or_str[1].startswith('.') else f'.{loc_or_str[1]}' elif loc_or_str[0] == 'css selector': loc_str = f':root>{self.tag}{loc_or_str[1]}' loc_or_str = loc_or_str[0], loc_str @@ -223,28 +223,27 @@ def execute_session_find(page_or_ele: BaseParser, if mode not in ['single', 'all']: raise ValueError("Argument mode can only be 'single' or 'all'.") loc_by, loc_str = loc - msg = result = first = None + result = None try: - if mode == 'single': - msg = 'Element not found.' - first = True - elif mode == 'all': - msg = 'Elements not found.' - first = False - + ele = None if loc_by == 'xpath': - ele = page_or_ele.xpath(loc_str, first=first) + if 'PyQuery' in str(type(page_or_ele.element)): # 从页面查找 + ele = page_or_ele.xpath(loc_str) + elif 'HtmlElement' in str(type(page_or_ele.element)): # 从元素查找 + elements = page_or_ele.element.xpath(loc_str) + ele = [Element(element=e, url=page_or_ele.url) for e in elements] else: - ele = page_or_ele.find(loc_str, first=first) + ele = page_or_ele.find(loc_str) if mode == 'single': - result = SessionElement(ele) if ele else None + result = SessionElement(ele[0]) if ele else None elif mode == 'all': result = [SessionElement(e) for e in ele] return result except: + # raise if show_errmsg: - print(msg, loc) + print('Element(s) not found.', loc) raise return [] if mode == 'all' else None