简体   繁体   中英

Parsing HTML with PHP DOMXpath

I want to retrieve event links and text from an external website using PHP and DOMXpath. The structure of the external website html is below;

<!-- first -->
<div class="col-sm-12 col-lg-3 me recording-item">
    <div class="recording-item-inner">
        <a class="col-sm-12 recording-name" href="/recordings/191">
        <div class="info">
            <b>Daily Event</b><br>
            <small>29 Jun 2020</small>
        </div></a>
    </div>
</div>
<!-- second -->
<div class="col-sm-12 col-lg-3 me recording-item">
    <div class="recording-item-inner">
        <a class="col-sm-12 recording-name" href="/recordings/190">
        <div class="info">
            <b>Daily Event B</b><br>
            <small>26 Jun 2020</small>
        </div></a>
    </div>
</div>
<!-- third -->
<div class="col-sm-12 col-lg-3 me recording-item">
    <div class="recording-item-inner">
        <a class="col-sm-12 recording-name" href="/recordings/189">
        <div class="info">
            <b>Daily Event C</b><br>
            <small>22 Jun 2020</small>
        </div></a>
    </div>
</div>

I'm trying to retrieve the latest 5 event names, dates and links. Currently I can get the latest (single) event using the code below.

<?php
function getEvents()
{

    $page = file_get_contents('https://example.com/events');
    $rootUrl = 'https://example.com';

    @$doc = new DOMDocument();
    @$doc->loadHTML($page);

    $xpath = new DomXPath($doc);

    $nodeList = $xpath->query("//div[@class='recording-item']");
    $node = $nodeList->item(0);

    $href = $xpath->evaluate("string(//div[@class='recording-item-inner']/a/@href)");
    $eventUrl = $rootUrl . $href;

    return $eventUrl;

}
?>

How can I amend this code so that it retrieves the 5 most recent event details and to print out a simple list of items;

<ul>
  <li>Event 1 - [name], [date], [href]</li>
  <li>Event 2 - [name], [date], [href]</li>
  <li>Event 3 - [name], [date], [href]</li>
  <li>Event 4 - [name], [date], [href]</li>
  <li>Event 5 - [name], [date], [href]</li>
</ul>

It can be done, but because of the limited xpath support, it's not the most elegant solution.

Starting from $nodeList ; given that your sample xml has only 3 events, this code will output the required information about the first two. Obviously, you can modify it for your actual code:

$nodeList = $xpath->query('//div[./div[@class="recording-item-inner"]]//div[@class="info"]');
$i = 1;
echo htmlspecialchars("<ul>", ENT_QUOTES);
echo "<br>";
foreach($nodeList as $result) { 
   if ($i++ > 2) break;
   echo htmlspecialchars("<li>", ENT_QUOTES);
   echo "Event 1 - " . $result->childNodes[1]->textContent . ",   ";
   echo $result->childNodes[4]->textContent . ",   ";
   echo $result->parentNode->getAttribute('href');
   echo htmlspecialchars("</li>", ENT_QUOTES);   
   echo "<br>";
   }
echo htmlspecialchars("</ul>", ENT_QUOTES);

Output:

<ul>
<li>Event 1 - Daily Event, 29 Jun 2020, /recordings/191</li>
<li>Event 1 - Daily Event B, 26 Jun 2020, /recordings/190</li>
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM