如何使用Python从Wikipedia文章中提取第一段?
例如,对于阿尔伯特·爱因斯坦来说:
Albert Einstein (pronounced /ˈælbərt ˈaɪnstaɪn/; German: [ˈalbɐt ˈaɪnʃtaɪn] ( listen); 14 March 1879 – 18 April 1955) was a theoretical physicist, philosopher and author who is widely regarded as one of the most influential and iconic scientists and intellectuals of all time. A German-Swiss Nobel laureate, Einstein is often regarded as the father of modern physics.[2] He received the 1921 Nobel Prize in Physics "for his services to theoretical physics, and especially for his discovery of the law of the photoelectric effect".[3]
我所做的是:
我写了一个Python库,目的是让这一切变得非常简单。在Github查看。
要安装它,请运行
然后要获得文章的第一段,只需使用
wikipedia.summary
函数。印刷品
就其工作方式而言,
wikipedia
向MediaWiki API的Mobile Frontend Extension发出请求,后者返回Wikipedia文章的移动友好版本。具体来说,通过传递参数prop=extracts&exsectionformat=plain
,MediaWiki服务器将解析Wikitext并返回您正在请求的文章的纯文本摘要,直到并包括整个页面文本。它还接受参数exchars
和exsentences
,这并不奇怪,限制了API返回的字符和语句的数量。不久前,我为获取纯文本的维基百科文章制作了两个类。我知道这不是最好的解决方案,但你可以根据自己的需要调整它:
wikipedia.py
wiki2plain.py
你可以这样使用它:
相关问题 更多 >
编程相关推荐