我正在尝试解析一个通过 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] => ''
)
关于php - 解析通过 javascript 填充内容的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14675081/