python访问Wikipedia

import wikipedia
# doc: https://pypi.org/project/wikipedia/

def searchWikiPedia(keyword, language) -> str:
    wikipedia.set_lang(language)
    result = wikipedia.page(keyword)
    return result.content


if __name__=="__main__":
    # 设置语言https://meta.wikimedia.org/wiki/List_of_Wikipedias
    lang = "zh"
    keyword = "new york"
    
    # 搜索wikipedia
    content = searchWikiPedia(keyword, lang)
    
    # 将内容保存到文件
    with open(f"{keyword}.txt", "w", encoding="utf-8") as file:
        file.write(content)

运行结果:

python访问Wikipedia_第1张图片

你可能感兴趣的:(1024程序员节,python)