Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/meme/jiki.py
2022-08-04 15:52:42 +08:00

38 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import traceback
from bs4 import BeautifulSoup
from config import Config
from core.elements import Url
from core.logger import Logger
from core.utils import get_url
async def jiki(term: str):
'''查询小鸡百科。
:param term: 需要查询的term。
:returns: 查询结果。'''
try:
api = 'https://jikipedia.com/search?phrase=' + term
webrender = Config('web_render')
if webrender:
api = webrender + 'source?url=' + api
html = await get_url(api, 200)
Logger.debug(html)
bs = BeautifulSoup(html, 'html.parser')
result = bs.select_one('[data-index="0"]')
title_ele = result.select_one(
'a.title-container.block.title-normal')
content_ele = result.select_one('.lite-card-content')
title = title_ele.get_text()
link = title_ele.get('href')
content = content_ele.get_text()
results = bs.select('.lite-card').__len__()
count = str(result) if results < 15 else '15+'
return f'[小鸡百科]{count}个结果):{title}\n{content}\n{str(Url(link))}'
except Exception:
traceback.print_exc()
return '[小鸡百科] 查询出错。'