mirror of
https://github.com/LiteyukiStudio/nonebot-plugin-marshoai.git
synced 2025-02-07 21:46:10 +08:00
修改, 但搜索功能完全无法使用了
This commit is contained in:
parent
ccb12f0f63
commit
086f21272f
@ -2,24 +2,34 @@ from nonebot.log import logger
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
import httpx
|
import httpx
|
||||||
|
import urllib.parse
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
from watchfiles import awatch
|
||||||
|
|
||||||
|
|
||||||
|
async def get_async_data (url):
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
return await client.get(url)
|
||||||
|
|
||||||
async def search(msg : str, num : int):
|
async def search(msg : str, num : int):
|
||||||
logger.info(f"搜索 : \"{msg}\"")
|
logger.info(f"搜索 : \"{msg}\"")
|
||||||
result = ""
|
result = ""
|
||||||
|
|
||||||
headers = {
|
# headers = {
|
||||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
|
# 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
|
||||||
}
|
# }
|
||||||
url = "https://mzh.moegirl.org.cn/index.php?search=" + msg
|
url = "https://mzh.moegirl.org.cn/index.php?search=" + urllib.parse.quote_plus(msg)
|
||||||
async with httpx.AsyncClient() as client:
|
# client = httpx.AsyncClient()
|
||||||
response = await client.get(url, headers = headers)
|
# async with client:
|
||||||
logger.info(response.headers.get('Location'))
|
# response = await client.post(url, headers = headers)
|
||||||
logger.info(f"连接萌娘百科中, 状态码 : {response.status_code}")
|
response = await get_async_data(url)
|
||||||
|
logger.info(f"连接\"{response.headers.get('location')}\"中, 状态码 : {response.status_code}")
|
||||||
|
|
||||||
|
# 正常搜索
|
||||||
|
if response.status_code == 200:
|
||||||
"""
|
"""
|
||||||
萌娘百科搜索页面结构
|
萌娘百科搜索页面结构
|
||||||
div.searchresults # 若无, 证明页面已重定向
|
div.searchresults
|
||||||
└── p ...
|
└── p ...
|
||||||
└── ul.mw-search-results # 若无, 证明无搜索结果
|
└── ul.mw-search-results # 若无, 证明无搜索结果
|
||||||
└── li # 一个搜索结果
|
└── li # 一个搜索结果
|
||||||
@ -29,68 +39,70 @@ async def search(msg : str, num : int):
|
|||||||
└── li ...
|
└── li ...
|
||||||
└── li ...
|
└── li ...
|
||||||
"""
|
"""
|
||||||
if response.status_code == 200:
|
soup = BeautifulSoup(response.text, 'html.parser')
|
||||||
soup = BeautifulSoup(response.text, 'html.parser')
|
|
||||||
|
|
||||||
# 检测div.searchresults, 是否已重定向
|
# 检测ul.mw-search-results, 是否有结果
|
||||||
if soup.find('div', class_='searchresults'):
|
if soup.find('ul', class_='mw-search-results'):
|
||||||
# 检测ul.mw-search-results, 是否有结果
|
ul_tag = soup.select('ul.mw-search-results')[0]
|
||||||
if soup.find('ul', class_='mw-search-results'):
|
li_tags = ul_tag.select('li')
|
||||||
ul_tag = soup.select('ul.mw-search-results')[0]
|
for li_tag in li_tags:
|
||||||
li_tags = ul_tag.select('li')
|
|
||||||
for li_tag in li_tags:
|
|
||||||
|
|
||||||
div_heading = li_tag.select('div.mw-search-result-heading')[0]
|
div_heading = li_tag.select('div.mw-search-result-heading')[0]
|
||||||
if div_heading:
|
if div_heading:
|
||||||
a_tag = div_heading.select('a')[0]
|
a_tag = div_heading.select('a')[0]
|
||||||
result += a_tag['title'] + "\n"
|
result += a_tag['title'] + "\n"
|
||||||
logger.info(f"搜索到 : \"{a_tag['title']}\"")
|
logger.info(f"搜索到 : \"{a_tag['title']}\"")
|
||||||
|
|
||||||
div_result = li_tag.find('div', class_='searchresult')
|
div_result = li_tag.find('div', class_='searchresult')
|
||||||
if div_result:
|
if div_result:
|
||||||
content = str(div_result).replace('<div class=\"searchresult\">', '').replace('</div>', '')
|
content = str(div_result).replace('<div class=\"searchresult\">', '').replace('</div>', '')
|
||||||
content = content.replace('<span class=\"searchmatch\">', '').replace('</span>', '')
|
content = content.replace('<span class=\"searchmatch\">', '').replace('</span>', '')
|
||||||
result += content + "\n\n"
|
result += content + "\n\n"
|
||||||
|
|
||||||
num -= 1
|
num -= 1
|
||||||
if num == 0:
|
if num == 0:
|
||||||
break
|
break
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# 无ul.mw-search-results, 无结果
|
# 无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:
|
else:
|
||||||
logger.error(f"网络错误, 状态码 : {response.status_code}")
|
logger.info("无结果")
|
||||||
return f"网络错误, 状态码 : {response.status_code}"
|
return "无结果"
|
||||||
|
|
||||||
|
# 重定向
|
||||||
|
elif response.status_code == 302:
|
||||||
|
logger.info(f"\"{msg}\"已被重定向")
|
||||||
|
# 读取重定向结果
|
||||||
|
soup = BeautifulSoup(response.text, 'html.parser')
|
||||||
|
logger.success("重定向成功")
|
||||||
|
logger.info(response)
|
||||||
|
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或302
|
||||||
|
else:
|
||||||
|
logger.error(f"网络错误, 状态码 : {response.status_code}")
|
||||||
|
return f"网络错误, 状态码 : {response.status_code}"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user