0

次のコードは、ウィキペディアのページから最初の段落を取得します。

<?
// 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.
}
?>

問題は、タイトルを PHP の変数にして、情報を「検索」できるようにしたい場合があることですが、クエリが常に正規の Wikipedia ページになるとは限りません。

たとえば、上記のコードで野球を検索すると、野球のページが表示されます。しかし、「マンダリン」を検索すると、次のように表示されます。

Mandarin may refer to any of the following:

しかし、オプションは表示されません。

私の質問は、ページが存在するかどうかを確認する方法はありますか?そうでない場合は、ウィキペディアからその可能性のあるオプションのリストを取得し、表示する最初のページを選択する方法はありますか?

4

1 に答える 1

0

80 年代に、XML および HTML ドキュメントの解析について言及したとき、ナンシー レーガンは次のように叫びました。

REGEXにノーと言いましょう!

ちょっと待って!私はそれで間違っているかもしれません。彼女は「ドラッグはやめて!」と言ったかもしれないと思います。彼女がそう言ったとき、おそらく XML や HTML 文書について考えていたとは思いません。しかし、もしそうなら、次の 2 つの理由から、XML と HTML の解析は PHP の DomDocument クラスを使用した方がよいという私に同意するでしょう。

  • 正規表現は、その目的ではあまり信頼できません。単一の文字がそれらを台無しにする可能性があり、ウェブマスターが正規表現パターンを役に立たなくするために行った変更。
  • 特にドキュメントから複数のアイテムを取得する必要がある場合、正規表現は遅くなります。DomDocument モデルはドキュメントを 1 回解析すると、簡単にアクセスできるようにすべてのデータがオブジェクトに含まれます。

「マンダリン」のページに行ったところ、次のことがわかりました。

<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');

URL を取得したら、次のページをリクエストするのは簡単です。ページにこの情報があるかどうかをテストする限り、それを理解できると思います。

この種のことを行う場合は、DomDocument クラスと xpath クエリの作成について学習する価値があります。

編集:

変数 $data は、ページの HTML を含む単なる文字列です。

于 2013-05-06T18:33:42.943 に答える