simplewikipedia.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:hangoutsbot 作者: das7pad 项目源码 文件源码
def wiki(bot, event, *args):
    """lookup a term on Wikipedia"""

    term = " ".join(args)
    if not term:
        return

    try:
        page = wikipedia.page(term, auto_suggest=False)

        summary = page.summary.strip()
        summary = summary.replace('\r\n', '\n').replace('\r', '\n')
        summary = re.sub('\n+', "\n", summary).replace('\n', '\n\n')
        source = _('<i>source: <a href="{}">{}</a></i>').format(page.url, page.url)

        html_text = '<b>"{}"</b>\n\n{}\n\n{}'.format(term, summary, source)
    except wikipedia.exceptions.PageError:
        html_text = _("<i>no entry found for {}</i>").format(term)
    except wikipedia.exceptions.DisambiguationError as e:
        exception_text = str(e).strip().replace("\n", "\n")
        html_text = "<i>{}</i>".format(exception_text)

    return html_text
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号