0

I want to scrape html with php and retrieve the js onclick url from specific table cells, the cells are always in identical pairs so I only need one url of every pair, for example:

<tr>
<td width='10' class='score' OnClick="window.location='page.99.html'">
<td width='10' class='score' OnClick="window.location='page.99.html'">
</tr>

I want to retrieve page.99.html from either these and carry on for however many pairs there are. I've been using this and can get the cell value but not the onclick propery:

$dom = new DOMDocument();
$res=$dom->loadHTML($html);
$xpath = new DomXPath($dom);
$cells = $xpath->query( '//td[@width="10"]');

foreach($cells as $cell) {
echo $cell->nodeValue;
}

Can anyone please tell me how to access this onclick url for each appropriate cell?

2
  • 1
    Have you tried var_dump($cell) to see what it contains? Commented Mar 5, 2014 at 21:06
  • I get a list of Dom objects - object(DOMElement)#3 (0) Commented Mar 5, 2014 at 21:12

2 Answers 2

2

Using simple dom:

$doc = str_get_html($str);
echo $doc->find('td[onclick]', 0)->onclick;
Sign up to request clarification or add additional context in comments.

Comments

1

This should do it :

$onClicks = array();

$dom->loadHTML($html);

foreach($dom->getElementsByTagName('td') as $td) { 
    if((int)$td->getAttribute('width') == 10) {
        $onClicks[] = $td->getAttribute('OnClick'); 
    }
} 

foreach($onClicks as $onClick) {
    // do something...
    echo $onClick;
}

1 Comment

sorry, this is probably really stupid - $onClicks is an array storing the various onclick attributes right? How do access or print each element of the array? I can see it's 16 items long as it should be but not echo or fwrite them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.