import requests
from bs4 import BeautifulSoup

from nonebot import on_command
from nonebot.plugin import on_keyword
from nonebot.rule import to_me
from nonebot.adapters.cqhttp import Bot, Event

baike = on_keyword(['百科'],priority=5)

@baike.handle()
async def baikeMain(bot:Bot, event:Event, state:dict):
    await baike.send("百度百科：给我一个关键词！",at_sender=True)

@baike.receive()
async def baikeTwo(bot:Bot, event:Event, state:dict):
    kw = str(event.get_message())
    url = f'https://baike.baidu.com/item/{kw}'
    headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
    }
    response = requests.get(url=url,headers=headers)
    text = response.text
    soup = BeautifulSoup(text,"html.parser")
    result = soup.find('meta',attrs={"name":"description"})
    if result==None:
        await baike.finish("抱歉！未查到相关信息！")
    else:    
        result = str(result['content'])
        await baike.finish(result)


import requests
from bs4 import BeautifulSoup

from nonebot import on_command
from nonebot.plugin import on_keyword
from nonebot.rule import to_me
from nonebot.adapters.cqhttp import Bot, Event

baike = on_keyword(['百科'],priority=5)

@baike.handle()
async def baikeMain(bot:Bot, event:Event, state:dict):
    await baike.send("百度百科：给我一个关键词！",at_sender=True)

@baike.receive()
async def baikeTwo(bot:Bot, event:Event, state:dict):
    kw = str(event.get_message())
    url = f'https://baike.baidu.com/item/{kw}'
    headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'
    }
    response = requests.get(url=url,headers=headers)
    text = response.text
    soup = BeautifulSoup(text,"html.parser")
    result = soup.find('meta',attrs={"name":"description"})
    if result==None:
        await baike.finish("抱歉！未查到相关信息！")
    else:    
        result = str(result['content'])
        await baike.finish(result)

