Question

Could someone please help me out.

I'm trying to get multiple href's from a page for exmaple.

The page

<div class="link__ttl">
    <a href="/watch-link-53767-934537" target="_blank" rel="nofollow">Version 1</a>
</div>
<div class="link__ttl">
    <a href="/watch-link-53759-934537" target="_blank" rel="nofollow">Version 1</a>
</div>

PHP Dom

$data = array();

$data['links'] = array();

$page = $this->curl->get($page);
$dom = new DOMDocument();
@$dom->loadHTML($page);

$divs = $dom->getElementsByTagName('div'); 
for($i=0;$i<$divs->length;$i++){   
    if ($divs->item($i)->getAttribute("class") == "link__ttl") {
        foreach ($divs as $div) {
            $link = $div->getElementsByTagName('a');
            $data['links'][] = $link->getAttribute("href");
        }
    }
}

But this don't same to work and i get a error

Call to undefined method DOMNodeList::getAttribute()

Could someone help me out here please thanks

Was it helpful?

Solution

You're testing divs for having the link__tt class, but then just for each all the divs. Take only the anchors from the divs that have the class.
Then you're trying to call getAttribute from a DOMNodeList, you need to get the underlying domnode to get the attribute.

$divs = $dom->getElementsByTagName('div'); 
for($i=0;$i<$divs->length;$i++){  
    $div = $divs->item($i);
    if ($div->getAttribute("class") == "link__ttl") {
            $link = $div->getElementsByTagName('a');
            $data['links'][] = $link->item(0)->getAttribute("href");
    }
}

Another solution is to use xpath

$path = new DOMXPath($dom);
$as = $path->query('//div[@class="link__ttl"]/a'); 
for($i=0;$i<$as->length;$i++){  
    $data['links'][] = $as->item($i)->getAttribute("href");
}

http://codepad.org/pX5qA1BB

OTHER TIPS

$link = $div->getElementsByTagName('a'); retrieves a LIST of Items where you cant's get an attribute-value "href" of...

try use of $link[0] instead of $link

Any part of a DOM is an node. The attributes are nodes, too, not just the elements. Using Xpath you can directly fetch an list of href attribute nodes.

$dom = new DOMDocument();
$dom->loadHtml($html);
$xpath = new DOMXpath($dom);

$result = [];
foreach ($xpath->evaluate('//div[@class = "link__ttl"]/a/@href') as $href)  {
  $result[] = $href->value;
}

var_dump($result);

Output: https://eval.in/150202

array(2) {
  [0]=>
  string(24) "/watch-link-53767-934537"
  [1]=>
  string(24) "/watch-link-53759-934537"
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top