简体   繁体   中英

Can I use cURL to grab a html table?

I'm trying to use cURL to grab an external web page to put into my own website, it's basically a "ladder" of a sports team, I contacted them, but they do not have a RSS feed of the ladder, so I'm trying to obtain the ladder by other means, is it possible to grab everything between < table > and < / table > using cURL? I can grab the page that I want using the following code, but I don't need anything else except for the 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 could help me out, that'd be great. Thanks

Leanne

Ok, so I managed to get it working using this (if anyone else wants to know)

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

:)

You'll need to use curl to grab the contents of the page and string processing to extract the table from the returned string.

A simple regex to start would be:

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

So if you take your example above, you'd do something like:

$page = curl_exec($ch);

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

This code will match the first table on the page. You'd need to tweak it to match exactly the HTML you want to extract.

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

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