我正在尝试解析一个通过javascript更新内部内容的页面。当我通过Firebug查看html时,它看起来如下:
<div id="productinfo">
<h2>
<span id="productname">Computer</span>
</h2>
<span id="servieidLabel" style=""> Service ID: </span>
<span id="snLabel" style="display: none"> Serial Number: </span>
<span id="servidno">12345ABCD</span>
然而,当我右键点击页面并看到来源时,下面是html:的结构
<div id="productinfo">
<h2><span id="productname"></span></h2>
<span id="serviceidLabel" style="display: none">
Service ID:
</span>
<span id="snLabel" style="display: none">
Serial Number:
</span>
<span id="servidno"></span><br>
javascript:
warrantyPage.warrantycheck.displayProductInfo('Computer', true,'12345ABCD', false, '');
我正在尝试解析并获得类似服务ID:12345ABCD的输出。请帮我怎么做。我尝试了下面的代码,但没有任何结果,因为很明显,服务id号不是html的一部分,而是由javascript 插入的
$servid = $xpath->query("//span[@id='servidno']");
foreach ($servid as $entry) {
echo "Service Id No:" ,$entry->nodeValue."<br />";
}
如果javascript填充函数总是有相同的参数顺序,您可以尝试解析它:
$text = "warrantyPage.warrantycheck.displayProductInfo('Computer', true,'12345ABCD', false, '');";
preg_match_all('/'[^']+'/', $text, $result);
print_r($result);
结果将是一个数组:
Array
(
[0] => Array
(
[0] => 'Computer'
[1] => '12345ABCD'
)
)
另一种没有正则表达式的方法:
$text = "warrantyPage.warrantycheck.displayProductInfo('Computer', true,'12345ABCD', false, '');";
$tail = substr($text, strpos($text, "displayProductInfo(") + 19 , -1);
$head = strstr($tail, ")", true);
$args = explode(',', $head);
$args将变成一个数组:
Array
(
[0] => 'Computer'
[1] => true
[2] => '12345ABCD'
[3] => false
[4] => ''
)