从维基百科文章中提取第一段 (Python) [英] Extract the first paragraph from a Wikipedia article (Python)

查看:53
本文介绍了从维基百科文章中提取第一段 (Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Python 从维基百科文章中提取第一段?

How can I extract the first paragraph from a Wikipedia article, using Python?

例如,对于阿尔伯特·爱因斯坦,那就是:

阿尔伯特·爱因斯坦(读作/ˈælbərtˈaɪnstaɪn/;德语:[ˈalbɐt ˈaɪnʃtaɪn]( 听);1879 年 3 月 14 日 – 4 月 18 日1955)是一位理论物理学家,哲学家和作家,广泛被认为是最有影响力和标志性的科学家和历代知识分子.一种德国-瑞士诺贝尔奖获得者爱因斯坦通常被认为是父亲现代物理学. [2]他收到了1921 年诺贝尔物理学奖因为他的为理论物理学服务,以及特别是因为他发现了光电效应定律".[3]

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]

推荐答案

前段时间我做了两个类来获取纯文本的维基百科文章.我知道它们不是最好的解决方案,但您可以根据自己的需要进行调整:

Some time ago I made two classes for get Wikipedia articles in plain text. I know that they aren't the best solution, but you can adapt it to your needs:

   wikipedia.py
   wiki2plain.py

    wikipedia.py
    wiki2plain.py

你可以这样使用它:

from wikipedia import Wikipedia
from wiki2plain import Wiki2Plain

lang = 'simple'
wiki = Wikipedia(lang)

try:
    raw = wiki.article('Uruguay')
except:
    raw = None

if raw:
    wiki2plain = Wiki2Plain(raw)
    content = wiki2plain.text

这篇关于从维基百科文章中提取第一段 (Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆