mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-marshoai.git
synced 2025-02-25 18:39:24 +08:00
接入萌娘百科搜索
This commit is contained in:
parent
4ca5324f69
commit
bb2f59eae9
@ -1,4 +1,10 @@
|
|||||||
from . import mg_Info
|
from . import mg_Info
|
||||||
|
from . import mg_Search
|
||||||
|
|
||||||
|
# meogirl
|
||||||
async def meogirl():
|
async def meogirl():
|
||||||
return mg_Info.meogirl()
|
return mg_Info.meogirl()
|
||||||
|
|
||||||
|
# Search
|
||||||
|
async def search(msg : str, num : int = 3):
|
||||||
|
return str(mg_Search.search(msg, num))
|
91
nonebot_plugin_marshoai/tools/marshoai-meogirl/mg_Search.py
Normal file
91
nonebot_plugin_marshoai/tools/marshoai-meogirl/mg_Search.py
Normal file
@ -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('<div class=\"searchresult\">', '').replace('</div>', '')
|
||||||
|
content = content.replace('<span class=\"searchmatch\">', '').replace('</span>', '')
|
||||||
|
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}"
|
@ -5,5 +5,28 @@
|
|||||||
"name" : "marshoai-meogirl__meogirl",
|
"name" : "marshoai-meogirl__meogirl",
|
||||||
"description" : "介绍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"
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
Loading…
x
Reference in New Issue
Block a user