使用PHP DOM解析器从Javascript代码中获取数据

时间:2014-03-19 18:46:15

标签: javascript php html parsing dom

我正在学习解析器,在看了Java解析器JSoup之后,我试图用PHP做同样的事情。我使用PHP简单HTML DOM(现在,如果它是最好的解决方案)从网址获取一些数据,但没有成功。如何使用PHP解析器从外部Web的下一个代码中获取8(near bar1)和18(near bar0)数字?

 <table>

 <tr><td><div name='divns6' id='divns6' style='position:relative;visibility:hidden;'       
    width='400' height='160'><table valign=botton cellpadding='0' cellspacing='0' 
    border='0'><tr     valign='bottom'>
  <td width=15 valign="bottom" height=150><a href="javascript:void(null)" 
         onMouseOver="changeImage('bar1','','47',2);activadiv('bar0','18');" 
         onMouseOut="changeImage('bar1','','47',0);desactivadiv('bar1');"><img 
         NAME="barra1" width="11px" height="47" border="0"></a></td>

  <td width=15 valign="bottom" height=150><a href="javascript:void(null)" 
         onMouseOver="changeImage('bar2','','21',2);activadiv('bar1','8');" 
         onMouseOut="changeImage('bar2','','21',0);desactivadiv('bar2');"><img 
         NAME="barra2" width="11px" height="21" border="0"></a></td>

  </td></tr></table>

这是我尝试过的内容,但它只显示整行,而不仅仅是数字:

<?php include ("simple_html_dom.php"); 

// Create DOM from URL or file
$html = file_get_html('http://www.myurl.com');


// Find all text blocks 


$ret = $html->find('a[onMouseOver]');



for ($count='0'; $count<count($ret);$count++) {

echo $ret[$count];


}




?>

使用JSoup,执行该操作的行是:String onMouseOver = doc.select(&#34; a&#34;)。attr(&#34; onMouseOver&#34;);

提前感谢您的时间和奉献精神。

1 个答案:

答案 0 :(得分:0)

最后,我自己找到了解决方案!!

<?php include ("simple_html_dom.php"); 

// Create DOM from URL or file
$html = file_get_html('http://www.myweb.com');


 // Find all text blocks 


 $ret = $html->find('a[onMouseOver]');



 for ($count='0'; $count<count($ret);$count++) {

       echo split("'", $ret[$count])[9]." ";



}




?>