From bb2f59eae9d9988362e63fd426d70ec7d67f3621 Mon Sep 17 00:00:00 2001 From: Twisuki Date: Wed, 11 Dec 2024 00:12:22 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=85=A5=E8=90=8C=E5=A8=98=E7=99=BE?= =?UTF-8?q?=E7=A7=91=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tools/marshoai-meogirl/__init__.py | 8 +- .../tools/marshoai-meogirl/mg_Search.py | 91 +++++++++++++++++++ .../tools/marshoai-meogirl/tools.json | 23 +++++ 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 nonebot_plugin_marshoai/tools/marshoai-meogirl/mg_Search.py diff --git a/nonebot_plugin_marshoai/tools/marshoai-meogirl/__init__.py b/nonebot_plugin_marshoai/tools/marshoai-meogirl/__init__.py index 4db722dd..4ad38666 100644 --- a/nonebot_plugin_marshoai/tools/marshoai-meogirl/__init__.py +++ b/nonebot_plugin_marshoai/tools/marshoai-meogirl/__init__.py @@ -1,4 +1,10 @@ from . import mg_Info +from . import mg_Search +# meogirl async def meogirl(): - return mg_Info.meogirl() \ No newline at end of file + return mg_Info.meogirl() + +# Search +async def search(msg : str, num : int = 3): + return str(mg_Search.search(msg, num)) \ No newline at end of file diff --git a/nonebot_plugin_marshoai/tools/marshoai-meogirl/mg_Search.py b/nonebot_plugin_marshoai/tools/marshoai-meogirl/mg_Search.py new file mode 100644 index 00000000..c8c22c11 --- /dev/null +++ b/nonebot_plugin_marshoai/tools/marshoai-meogirl/mg_Search.py @@ -0,0 +1,91 @@ +from nonebot.log import logger + +import re +import requests +from bs4 import BeautifulSoup + +def search(msg : str, num : int): + logger.info(f"搜索 : \"{msg}\"") + result = "" + + url = "https://mzh.moegirl.org.cn/index.php?search=" + msg + response = requests.get(url) + logger.info(f"连接萌娘百科中, 状态码 : {response.status_code}") + + """ + 萌娘百科搜索页面结构 + div.searchresults # 若无, 证明页面已重定向 + └── p ... + └── ul.mw-search-results # 若无, 证明无搜索结果 + └── li # 一个搜索结果 + └── div.mw-search-result-heading > a # 标题 + └── div.mw-searchresult # 内容 + └── div.mw-search-result-data + └── li ... + └── li ... + """ + if response.status_code == 200: + soup = BeautifulSoup(response.text, 'html.parser') + + # 检测div.searchresults, 是否已重定向 + if soup.find('div', class_='searchresults'): + # 检测ul.mw-search-results, 是否有结果 + if soup.find('ul', class_='mw-search-results'): + ul_tag = soup.select('ul.mw-search-results')[0] + li_tags = ul_tag.select('li') + for li_tag in li_tags: + + div_heading = li_tag.select('div.mw-search-result-heading')[0] + if div_heading: + a_tag = div_heading.select('a')[0] + result += a_tag['title'] + "\n" + logger.info(f"搜索到 : \"{a_tag['title']}\"") + + div_result = li_tag.find('div', class_='searchresult') + if div_result: + content = str(div_result).replace('
', '').replace('
', '') + content = content.replace('', '').replace('', '') + result += content + "\n\n" + + num -= 1 + if num == 0: + break + return result + + # 无ul.mw-search-results, 无结果 + else: + logger.info("无结果") + return "无结果" + + # 无div.searchresults, 重定向 + else: + logger.info(f"\"{msg}\"已被重定向") + num = 0 + + """ + 萌娘百科重定向介绍页面结构 + div#mw-content-text + └── div.mw-parser-output # 介绍页面 + └── .... + └── p ? # 可能存在的空p + └── p # 人物介绍 + └── ... + """ + if soup.find('div', class_='mw-parser-output'): + div = soup.find('div', class_='mw-parser-output') + p_tags = div.select('p') + for p_tag in p_tags: + p = str(p_tag) + p = re.sub(r'<.*?>', '', p) + if p != '': + result += str(p) + "/n" + + num += 1 + if num >= 5: + break + return result + + # 状态码非200 + else: + logger.error(f"网络错误, 状态码 : {response.status_code}") + return f"网络错误, 状态码 : {response.status_code}" diff --git a/nonebot_plugin_marshoai/tools/marshoai-meogirl/tools.json b/nonebot_plugin_marshoai/tools/marshoai-meogirl/tools.json index 3a4976a7..94e8596d 100644 --- a/nonebot_plugin_marshoai/tools/marshoai-meogirl/tools.json +++ b/nonebot_plugin_marshoai/tools/marshoai-meogirl/tools.json @@ -5,5 +5,28 @@ "name" : "marshoai-meogirl__meogirl", "description" : "介绍Meogirl" } + }, + { + "type" : "function", + "function" : { + "name" : "marshoai-meogirl__search", + "description" : "在萌娘百科中搜索(仅用户指定在萌娘百科中搜索才调用此函数)", + "parameters" : { + "type" : "object", + "properties" : { + "msg" : { + "type": "string", + "description": "搜索关键词" + }, + "num" : { + "type": "integer", + "description": "数据显示条数, 默认3, 可留空" + } + } + }, + "required": [ + "msg" + ] + } } ] \ No newline at end of file