Fetch excerpt from Wikipedia article?

后端 未结 4 1387
刺人心
刺人心 2020-12-08 17:37

I\'ve been up and down the Wikipedia API, but I can\'t figure out if there\'s a nice way to fetch the excerpt of an article (usually the first paragraph). It would

4条回答
  •  萌比男神i
    2020-12-08 18:04

    I found no way of doing this through the API, so I resorted to parsing HTML, using PHP's DOM functions. This was pretty easy, something among the lines of:

    $doc = new DOMDocument();
    $doc->loadHTML($wikiPage);
    $xpath = new DOMXpath($doc);
    $nlPNodes = $xpath->query('//div[@id="bodyContent"]/p');
    $nFirstP = $nlPNodes->item(0);
    $sFirstP = $doc->saveXML($nFirstP);
    echo $sFirstP; // echo the first paragraph of the wiki article, including 

提交回复
热议问题