Php JSON解析,如果Wikipedia有多个选项,请选择要显示的第一页

Php JSON解析,如果Wikipedia有多个选项,请选择要显示的第一页,php,json,parsing,wikipedia,Php,Json,Parsing,Wikipedia,下面的代码从Wikipedia页面获取第一段 <? // action=parse: get parsed text // page=Baseball: from the page Baseball // format=json: in json format // prop=text: send the text content of the article // section=0: top content of the page $find = $_GET['find']; $u

下面的代码从Wikipedia页面获取第一段

<?
// action=parse: get parsed text
// page=Baseball: from the page Baseball
// format=json: in json format
// prop=text: send the text content of the article
// section=0: top content of the page

$find = $_GET['find'];

$url = 'http://en.wikipedia.org/w/api.php?action=parse&page=baseball&format=json&prop=text&section=0';
$ch = curl_init($url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "TestScript"); // required by wikipedia.org server; use YOUR user agent with YOUR contact information. (otherwise your IP might get blocked)
$c = curl_exec($ch);

$json = json_decode($c);

$content = $json->{'parse'}->{'text'}->{'*'}; // get the main text content of the query (it's parsed HTML)

// pattern for first match of a paragraph
$pattern = '#<p>(.*?)</p>#s'; // http://www.phpbuilder.com/board/showthread.php?t=10352690
if(preg_match_all($pattern, $content, $matches))
{
    // print $matches[0]; // content of the first paragraph (including wrapping <p> tag)
    echo "Wikipedia:<br>";
    print strip_tags(implode("\n\n",$matches[1])); // Content of the first paragraph without the HTML tags.
}
?>
但它没有显示任何选项


我的问题是,是否有办法检查该页面是否存在,如果不存在,从维基百科获取可能存在的选项列表,然后选择要显示的第一个页面

早在80年代,当提到解析XML和HTML文档时,Nancy Reagan大声喊道:

对REGEX说不就行了

等等!我可能弄错了。我想她可能会说,“对毒品说不就行了!”我想她说这话的时候可能没有想到XML或HTML文档。但如果她是的话,我相信她会同意我的观点,解析XML和HTML最好使用PHP的DomDocument类,原因有两个:

  • 正则表达式在这方面不是很可靠。一个字符就可以把它们扔掉,网站管理员为了使你的正则表达式模式无效而做的任何更改
  • 正则表达式速度很慢,尤其是当您必须从文档中获取多个项时。DomDocument模型解析文档一次,然后所有数据都包含在一个对象中,以便于访问
我进入“普通话”页面,发现以下内容:

<h2>
    <span class="editsection">[<a href="/w/index.php?title=Mandarin&amp;action=edit&amp;section=1" title="Edit section: Officials">edit</a>]</span>
    <span class="mw-headline" id="Officials">Officials</span>
</h2>
<ul>
    <li><a href="/wiki/Mandarin_(bureaucrat)" title="Mandarin (bureaucrat)">Mandarin (bureaucrat)</a>, a bureaucrat of Imperial China (the original meaning of the word), Vietnam, and by analogy, any senior government bureaucrat</li>
</ul>
一旦你有了URL,请求下一个页面就很简单了。至于测试一个页面是否有这些信息,我想你可以弄清楚

如果您打算做这类事情,那么了解DomDocument类和进行xpath查询是非常值得的

编辑:


变量$data只是一个包含页面中HTML的字符串。

关键是它确实存在-只是它是一个消歧页面(您可以从类别中推断)。对于一个不存在的页面的响应看起来应该如何处理一个现有的页面呢?如何获取第一个链接并使用该链接运行脚本?只需搜索第一个出现的
,它表示对非对象调用了getAttribute()。知道这意味着什么吗?@derekshell-由“$res->item(0)”指定的对象不包含“href”属性。我使用上面发布的内容运行了代码,该对象确实包含“href”属性,并按预期返回了链接。因此,如果您遇到这样的错误,这意味着您没有正确地传递HTML数据,或者代码中存在其他错误。在我这边,没有错误。@derekshull-我只是通过复制和粘贴上面的代码进行了双重检查,它工作得非常好。如果你仍然有问题,请给我更多的信息。
<h2>
    <span class="editsection">[<a href="/w/index.php?title=Mandarin&amp;action=edit&amp;section=1" title="Edit section: Officials">edit</a>]</span>
    <span class="mw-headline" id="Officials">Officials</span>
</h2>
<ul>
    <li><a href="/wiki/Mandarin_(bureaucrat)" title="Mandarin (bureaucrat)">Mandarin (bureaucrat)</a>, a bureaucrat of Imperial China (the original meaning of the word), Vietnam, and by analogy, any senior government bureaucrat</li>
</ul>
$doc = new DOMDocument();
//load HTML string into document object
if ( ! @$doc->loadHTML($data)){
    return FALSE;
}
//create XPath object using the document object as the parameter
$xpath = new DOMXPath($doc);
$query = "//span[@class='editsection']/a";
//XPath queries return a NodeList
$res = $xpath->query($query);
$link = $res->item(0)->getAttribute('href');