get HTML element by attribute value in php

后端 未结 3 419
小蘑菇
小蘑菇 2021-02-04 12:41

I need to extract some data from a webpage with php. The part that I\'m interested in is structured similarly to this:



        
3条回答
  •  -上瘾入骨i
    2021-02-04 13:13

    Just continue on target attributes which aren't fruit, and then add the textContent of the elements to an array.

    $nodes = array();
    
    for ($i; $i < $a->length; $i++) {
        $attr = $a->item($i)->getAttribute('target');
    
        if ($attr != 'fruit') {
            continue;
        }
    
        $nodes[] = $a->item($i)->textContent;
    }
    

    $nodes now contains all the nodes of the elements which have their target attribute set to fruit.

提交回复
热议问题