我正在尝试解析通过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 />";
}
答案 0 :(得分:0)
如果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] => ''
)