Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 捕获空白节点DOM并赋值_Php_Html_Xpath_Scrape - Fatal编程技术网

Php 捕获空白节点DOM并赋值

Php 捕获空白节点DOM并赋值,php,html,xpath,scrape,Php,Html,Xpath,Scrape,好吧,我对DOM有点生疏,但到目前为止,我一直设法拼凑出一个半可行的解决方案 使用xpath,我在网页中查找关键元素,并在每个实例中循环,这很好,直到到达一个空节点 因此,在构建阵列时,我假设一个元素有20个节点,但另一个元素只有14个节点,因为img并不总是在那里 实际上我有一个数组,看起来像这样 Array ( [0] => Array ( [item] => PV10923 [img] => image1.jpg ) [1

好吧,我对DOM有点生疏,但到目前为止,我一直设法拼凑出一个半可行的解决方案

使用xpath,我在网页中查找关键元素,并在每个实例中循环,这很好,直到到达一个空节点

因此,在构建阵列时,我假设一个元素有20个节点,但另一个元素只有14个节点,因为img并不总是在那里

实际上我有一个数组,看起来像这样

Array
(
[0] => Array
    (
        [item] => PV10923
        [img] => image1.jpg
    )

[1] => Array
    (
        [item] => PV10924
        [img] => image2.jpg
    )

[2] => Array
    (
        [item] => PV10925
        [img] => image3.jpg
    )

[3] => Array
    (
        [item] => PV10926
        [img] => image4.jpg
    )

[4] => Array
    (
        [item] => PV10927
        [img] => 
    )

[5] => Array
    (
        [item] => PV10928
        [img] => 
    )

[6] => Array
    (
        [item] => PV10929
        [img] => 
    )

)
<div id="item">
<h2>PV PV10924</h2>
<p>
<a href="http://www.example.com"><img src="image4.jpg">
</p>
</div>
<div id="item">
<h2>PV PV10925</h2>
<p>
&nbsp; (assign a value)
</p>
</div>
<div id="item">
<h2>PV PV10926</h2>
<p>
<a href="http://www.example.com"><img src="image5.jpg">
 </p>
 </div>
实际上应该是这样的

    Array
   (
[0] => Array
    (
        [item] => PV10923
        [img] => image1.jpg
    )

[1] => Array
    (
        [item] => PV10924
        [img] => image2.jpg
    )

[2] => Array
    (
        [item] => PV10925
        [img] =>  
    )

[3] => Array
    (
        [item] => PV10926
        [img] =>  
    )

[4] => Array
    (
        [item] => PV10927
        [img] => 
    )

[5] => Array
    (
        [item] => PV10928
        [img] => image3.jpg
    )

[6] => Array
    (
        [item] => PV10929
        [img] => Image4.jpg
    )

  )
现在网页源代码如下所示

Array
(
[0] => Array
    (
        [item] => PV10923
        [img] => image1.jpg
    )

[1] => Array
    (
        [item] => PV10924
        [img] => image2.jpg
    )

[2] => Array
    (
        [item] => PV10925
        [img] => image3.jpg
    )

[3] => Array
    (
        [item] => PV10926
        [img] => image4.jpg
    )

[4] => Array
    (
        [item] => PV10927
        [img] => 
    )

[5] => Array
    (
        [item] => PV10928
        [img] => 
    )

[6] => Array
    (
        [item] => PV10929
        [img] => 
    )

)
<div id="item">
<h2>PV PV10924</h2>
<p>
<a href="http://www.example.com"><img src="image4.jpg">
</p>
</div>
<div id="item">
<h2>PV PV10925</h2>
<p>
&nbsp; (assign a value)
</p>
</div>
<div id="item">
<h2>PV PV10926</h2>
<p>
<a href="http://www.example.com"><img src="image5.jpg">
 </p>
 </div>

PV PV10924

PV PV10925 (指定一个值)

PV PV10926

我一直在寻找是否有办法捕获父节点,然后执行if语句查看子节点是否存在,然后执行xpath if not赋值x节点

阅读困难不是我的强项,但相信我,我正在努力


有谁能告诉我实现这一点的最佳路线/方法吗……

您可以检查特定元素的后代。例如:

$sample_markup = '<div id="item"><h2>PV PV10924</h2><p><a href="http://www.example.com"><img src="image4.jpg"></a></p></div><div id="item"><h2>PV PV10925</h2><p>&nbsp; (assign a value)</p></div><div id="item"><h2>PV PV10926</h2><p><a href="http://www.example.com"><img src="image5.jpg"></a> </p> </div>';
// using the sample markup above
$dom = new DOMDocument();
libxml_use_internal_errors(true); // handle errors
$dom->loadHTML($sample_markup);
libxml_clear_errors();
$xpath = new DOMXpath($dom);

$data = array();
$elements = $xpath->query('//div[@id="item"]');
foreach($elements as $e) {
    $item = $xpath->evaluate('string(.//h2/text())', $e);
    // checking
    $check = $xpath->evaluate('count(.//*[descendant::a])', $e);
    if($check > 0) {
        $image = $xpath->evaluate('string(.//a/img/@src)', $e);
    } else {
        $image = 'test.jpg';
    }
    $data[] = array('item' => $item, 'image' => $image);
}

echo '<pre>';
print_r($data);
$sample_markup='PV PV10924

PV PV10925(赋值)

PV PV10926

; //使用上面的示例标记 $dom=新的DOMDocument(); libxml_使用_内部_错误(true);//处理错误 $dom->loadHTML($sample\u标记); libxml_clear_errors(); $xpath=newdomxpath($dom); $data=array(); $elements=$xpath->query('//div[@id=“item”]”); foreach($e){ $item=$xpath->evaluate('string(.//h2/text())',$e); //检查 $check=$xpath->evaluate($count(.//*[genderant::a]),$e); 如果($check>0){ $image=$xpath->evaluate(../a/img/@src)字符串,$e); }否则{ $image='test.jpg'; } $data[]=数组('item'=>$item,'image'=>$image); } 回声'; 打印(数据);

那么您想检查inside
是否有一个带有img的anchor子项?这真的是标记吗?它有相同的多个ID,本质上不会使senseYes为yes,如果有一个子项生效,则通过if语句preform,否则捕获将分配一个默认值,以便构建与网页匹配的数组。我相信你下面的代码片段不仅仅是为了测试它。我以前没有见过“评估”功能,所以我也要读一些关于咖啡的书,咖啡也是保持大脑专注所必需的。是的,你可以测试一下。就像名称本身只是计算xpath查询一样进行计算。很高兴这有助于我在使用一个类时做了一个小的调整,所以我必须在类中包含求值的方法,所以我使用for函数运行您的foreach,然后将您的foreach数组添加到我的for中,它非常匹配。