获取维基百科文章的第一行

the*_*ega 12 parsing wikipedia wikipedia-api

我有一篇维基百科文章,我想从文章中获取第一行z(或前x个字符,或前y个字,无关紧要).

问题:我可以获得源Wiki-Text(通过API)或解析的HTML(通过直接HTTP-Request,最终在打印版本上)但是如何找到显示的第一行?Normaly源(html和wikitext)从信息框和图像开始,第一个要显示的真实文本在代码中的某处.

例如: 维基百科上的阿尔伯特爱因斯坦(印刷版).看看代码,第一个真正的文本行"Albert Einstein(发音为/ælbərtaɪnstaɪn/;德语:[albɐtaɪ̯nʃtaɪ̯n]; 1879年3月14日至1955年4月18日)是一位理论物理学家." 不是一开始.这同样适用于Wiki-Source,它以相同的信息框开头,依此类推.

那么你将如何完成这项任务呢?编程语言是java,但这应该不重要.

我想到的解决方案是使用xpath查询,但是这个查询处理所有边界情况会相当复杂.[更新]没有那么复杂,请参阅下面的解决方案![/ update]

谢谢!

oct*_*pus 10

你不需要.

API的exintro参数仅返回文章的第一个(第零个)部分.

示例: api.php?action = query&prop = extracts&exintro&explaintext&titles = Albert%20Einstein

还有其他参数:

  • exchars 以字符为单位的提取长度.
  • exsentences 要返回的句子数.
  • exintro 仅返回第0部分.
  • exsectionformat 用于明文提取的标题格式:

    wiki — e.g., == Wikitext ==
    plain — no special decoration
    raw — this extension's internal representation
    
    Run Code Online (Sandbox Code Playgroud)
  • exlimit 要返回的最大提取数.由于摘录生成可能很慢,因此对于仅限介绍的提取,限制的上限为20,对于整页提取,限制的上限为1.
  • explaintext 返回纯文本提取.
  • excontinue 当有更多结果可用时,请使用此参数继续.

资料来源:https://www.mediawiki.org/wiki/Extension:MobileFrontend#prop.3Dextracts


the*_*ega 0

我制定了以下解决方案:

在 XHTML 源代码上使用 XPath 查询(我采用了打印版本,因为它更短,但它也适用于普通版本)。

//html/body//div[@id='bodyContent']/p[1]
Run Code Online (Sandbox Code Playgroud)

这适用于德语和英语维基百科,我还没有找到一篇文章不输出第一段。解决方案也相当快,我还想过只取XHTML的前x个字符,但这会使XHTML无效。

如果有人正在搜索 Java 代码,那就是:

private static DocumentBuilderFactory dbf;

static {
    dbf = DocumentBuilderFactory.newInstance();
    dbf.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
}

private static XPathFactory xpathf = XPathFactory.newInstance();
private static String xexpr = "//html/body//div[@id='bodyContent']/p[1]";


private static String getPlainSummary(String url) {
    try {
        // Open Wikipage
        URL u = new URL(url);
        URLConnection uc = u.openConnection();
        uc.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1) Gecko/20090616 Firefox/3.5");
        InputStream uio = uc.getInputStream();
        InputSource src = new InputSource(uio);

        // Construct Builder
        DocumentBuilder builder = dbf.newDocumentBuilder();
        Document docXML = builder.parse(src);

        // Apply XPath
        XPath xpath = xpathf.newXPath();
        XPathExpression xpathe = xpath.compile(xexpr);
        String s = xpathe.evaluate(docXML);

        // Return Attribute
        if (s.length() == 0) {
            return null;
        } else {
            return s;
        }
    }
    catch (IOException ioe) {
        logger.error("Cant get XML", ioe);
        return null;
    }
    catch (ParserConfigurationException pce) {
        logger.error("Cant get DocumentBuilder", pce);
        return null;
    }
    catch (SAXException se) {
        logger.error("Cant parse XML", se);
        return null;
    }
    catch (XPathExpressionException xpee) {
        logger.error("Cant parse XPATH", xpee);
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

通过调用来使用它getPlainSummary("http://de.wikipedia.org/wiki/Uma_Thurman");