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('