代码之家  ›  专栏  ›  技术社区  ›  theomega

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

  •  11
  • theomega  · 技术社区  · 17 年前

    我收到一篇维基百科文章,我想从文章中提取第一个z行(或者第一个x字符,或者第一个y单词,都不重要)。

    问题是:我可以获取源Wiki文本(通过API)或解析的HTML(通过直接HTTP请求,最终在打印版本上),但如何找到显示的第一行?通常,源代码(html和wikitext)以信息框和图像开始,第一个显示的真实文本位于代码的下面。

    例如: Albert Einstein on Wikipedia (印刷版)。查看代码中的第一行实际文本 现在还没有开始。这同样适用于 Wiki-Source ,它以相同的信息框开始,依此类推。

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

    我想到的一个解决方案是使用xpath查询,但该查询对于处理所有边界情况来说相当复杂。[更新]没有那么复杂,请参见下面我的解决方案![/更新]

    谢谢

    9 回复  |  直到 11 年前
        1
  •  16
  •   octosquidopus Martijn Pieters    13 年前

    你不需要。

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

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

    还有其他参数:

    • exchars 以字符为单位的摘录长度。
    • exsentences 要返回的句子数。
    • 埃森特罗 只返回第0节。
    • exsectionformat 用于纯文本摘录的节标题格式:

      wiki — e.g., == Wikitext ==
      plain — no special decoration
      raw — this extension's internal representation
      
    • exlimit 要返回的最大提取数。因为摘录的生成可能很慢,所以仅简介摘录的限制为20,整页摘录的限制为1。
    • explaintext 返回纯文本摘录。
    • excontinue

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

        2
  •  3
  •   Anand Chitipothu    17 年前

    我也有同样的需要,为此编写了一些Python代码。

    代码位于 http://github.com/anandology/sandbox/blob/master/wikisnip/wikisnip.py .

        3
  •  2
  •   Community Mohan Dere    6 年前

    download 2.5GB ),它为所有文章提供您想要的信息。

        4
  •  1
  •   Aaron Digulla    17 年前

    WikiText 或者随附的解析器 XWiki .

        5
  •  1
  •   Geo    17 年前

    我在Firefox上打开了Albert Einstein的文章,点击了View source。使用HTML解析器进行解析非常容易。你应该把注意力集中在 <p>

        6
  •  1
  •   Gabriel Guimarães    17 年前

    例如,如果结果是字符串 您可以找到以下文本:

    <div id="bodyContent">
    

    在索引之后,你会找到第一个

    <p>
    

    这就是你提到的第一段的索引。

    试试这个网址 Link to the content (just works in the browser)

        7
  •  0
  •   Joey Gumbo    17 年前

    好的,当使用Wiki源代码本身时,您可以从一开始就删除所有模板。对于大多数顶部有信息框或一些消息的文章来说,这可能已经足够好了。

    另一种可能更可靠的方法是采用第一种方法的内容 <p> 直接出现在文章文本中的标记(因此不嵌套在表中)。这应该在一开始就去掉信息框和其他东西,因为它们可能是(我不太确定) <table> <div>

        8
  •  0
  •   Tim Palak Chaudhary    17 年前

    正如您所期望的,您可能最终必须解析源代码、已编译的HTML或两者。但是, Wikipedia:Lead_section 可能会给你一些关于在好的文章中期望什么的指示。

        9
  •  0
  •   insumity    13 年前

    我制定了以下解决方案: (我选择打印版是因为它较短,但它也适用于普通版)。

    //html/body//div[@id='bodyContent']/p[1]
    

    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;
        }
    }
    

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

    推荐文章