fix(lxml): Adjust HTML element checks to prevent FutureWarning

This commit is contained in:
InfinityPacer
2024-08-31 02:14:52 +08:00
parent 9ab852c1ad
commit 93801e857e
13 changed files with 35 additions and 23 deletions

View File

@@ -1,5 +1,7 @@
from lxml import etree
from app.utils.string import StringUtils
class SiteUtils:
@@ -11,7 +13,7 @@ class SiteUtils:
:return:
"""
html = etree.HTML(html_text)
if not html:
if not StringUtils.is_valid_html_element(html):
return False
# 存在明显的密码输入框,说明未登录
if html.xpath("//input[@type='password']"):
@@ -39,7 +41,7 @@ class SiteUtils:
:return True已签到 False未签到
"""
html = etree.HTML(html_text)
if not html:
if not StringUtils.is_valid_html_element(html):
return False
# 站点签到支持的识别XPATH
xpaths = [

View File

@@ -795,3 +795,13 @@ class StringUtils:
:return: 如果输入值不是 None返回去除空白字符后的字符串否则返回 None
"""
return value.strip() if value is not None else None
@staticmethod
def is_valid_html_element(elem) -> bool:
"""
检查elem是否为有效的HTML元素。元素必须为非None并且具有非零长度。
:param elem: 要检查的HTML元素
:return: 如果elem有效非None且长度大于0返回True否则返回False
"""
return elem is not None and len(elem) > 0