I'm currently working on a fantasy sports site, and I want to be able to pull basic stats from another site. (I don't have much experience with XML or pulling data from other sites).
I inspected the element to gain it's XPath:
Which gave me: //*[@id="cp1_ctl01_pnlPlayerStats"]/table[1]/tbody/tr[4]/td[18]
I've looked into a couple methods of trying to pull the info and came up with this:
But I just end up with empty elements in my table within my site:
Here's My Code:
$doc = new DOMDocument();
@$doc->loadHTMLFile($P_RotoLink);
$xpath = new DOMXpath($doc);
$elements = $xpath->query('//* [@id="cp1_ctl01_pnlPlayerStats"]/table[1]/tbody/tr[4]/td[18]');
if (!is_null($elements)) {
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
A few things I've tried have thrown me errors, and any time I finally get pass them or suppress them I get empty content. I've tried a bunch of different formats but none seem to give me the desired content.
Edit: Here's the source HTML, I want to grab the value within the td (13.0).
Edit 2: So this is what I'm trying now:
$html = file_get_contents($P_RotoLink);
$doc = new DOMDocument;
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_use_internal_errors(false);
$xpath = new DOMXpath( $doc);
foreach ($xpath->query('//*[@id="cp1_ctl01_pnlPlayerStats"]/table//tr[4]/td[18]') as $node) {
$ppg = substr($node->textContent,0,3);
echo $ppg;
}
The problem is that the table in the screenshot doesn't have tbody
node, but your XPath expression includes tbody
which causes DOMXPath::query
to return an empty list of nodes. I suggest ignoring tbody
and fetching only rows with //tr
.
Example
$html = <<<'HTML'
<div id="cp1_ctl01_pnlPlayerStats">
<table>
<tr></tr>
<tr>
<td><span>0.9</span>1.0<span>3.0</span></td><td>2.0</td>
</tr>
</table>
</div>
HTML;
$doc = new DOMDocument();
$doc->loadHTML($html);
$xp = new DOMXPath($doc);
$expr = '//*[@id="cp1_ctl01_pnlPlayerStats"]/table//tr[2]/td[1]/text()';
$td = $xp->query($expr);
if ($td->length) {
var_dump($td[0]->nodeValue);
}
Output
string(3) "1.0"
The text()
function selects all text node children of the context node.
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.