Can I use cURL to capture an html table?

I am trying to use cURL to capture an external web page for posting on my own website, it is basically a “ladder” of a sports team, I contacted them, but they don’t have a ladder RSS feed, so I try to get the ladder by other means , is it possible to capture everything between <table> and </table> using cURL? I can capture the page I want using the following code, but I don't need anything but an HTML table.

$ch = curl_init ("http://www.sportingpulse.com/rpt_ladder.cgi?results=N&round=15&client=1-3909-47801-81021-6151461&pool=-1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $page = curl_exec($ch);

If someone can help me, it will be great. Thanks

Liane

+3
source share
3 answers

curl , .

:

/<table>(.*)<\/table/s

, , - :

$page = curl_exec($ch);

if (preg_match("/<table>(.*)<\/table/s", $page, $matches)) {
    echo $matches[1];
}

. , HTML.

+2

, ( - )

$ch = curl_init ("http://www.sportingpulse.com/rpt_ladder.cgi?results=N&round=15&client=1-3909-47801-81021-6151461&pool=-1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$page = curl_exec($ch);

preg_match('#<table[^>]*>(.+?)</table>#is', $page, $matches);
foreach ($matches as &$match) {
    $match = $match;
}
echo '<table>';
    echo $matches[1];
echo '</table>';

:)

+5

An alternative for a pure regex would be to use DOMDocument and xPath. This turns the entire document into an object and simplifies working with the contents of the table.

+1
source

Source: https://habr.com/ru/post/1714581/


All Articles