1

I want to get the links "http://www.w3schools.com/default.asp" & "http://www.google.com" from this webpage.I want the links of <a> tags inside <div class="link">,there are many other <a> tags in this page and I don't want them. How can I retrieve the particular links only? Can anyone help me?

<div class="link">
<a href="http://www.w3schools.com/default.asp">
<h4>W3 Schools</h4>
</a>
</div>
<div class="link">
<a href="http://www.google.com">
<h4>Google</h4>
</a>
</div>
1

3 Answers 3

5

Use a DOM Parser such as DOMDocument to achieve this:

$dom = new DOMDocument;
$dom->loadHTML($html); // $html is a string containing the HTML

foreach ($dom->getElementsByTagName('a') as $link) {
    echo $link->getAttribute('href').'<br/>';
}

Output:

http://www.w3schools.com/default.asp
http://www.google.com

Demo.


UPDATE: If you only want the links inside the specific <div>, you can use an XPath expression to find the links inside the div, and then loop through them to get the href attribute:

$dom = new DOMDocument;
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$links_inside_div = $xpath->query("//*[contains(@class, 'link')]/a");

foreach ($links_inside_div as $link) {
    echo $link->getAttribute('href').'<br/>';
}

Demo.

3
  • I want to get only those <a> tag links inside <div class="link"> Note:There are many other <a href=""> tags are there
    – sarath
    Commented Dec 19, 2013 at 11:54
  • Warning: DOMDocument::loadHTML(): htmlParseEntityRef: expecting ';' in Entity, line: 38 in C:\xampp\htdocs\fetch\bkadafetch.php on line 7 This is the output when I executed the code, line 7 contains $dom->loadHTML($html);
    – sarath
    Commented Dec 19, 2013 at 12:32
  • Although, it's not considered a very good practice, you can use @$dom->loadHTML($html); to suppress the warnings. A better idea might be to use libxml_internal_errors(). See this answer. Commented Dec 19, 2013 at 12:40
1
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('a') as $node)
{
  echo $node->nodeValue.': '.$node->getAttribute("href")."\n";
}
1

You can use snoopy PHP class . Snoopy is a PHP class that simulates a web browser. It automates the task of retrieving web page content and posting forms, http://sourceforge.net/projects/snoopy/

Otherwise try to using Jquery

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
 <script type="text/javascript">
    $( document ).ready(function() {
         $( ".link a" ).each(function( index ) {
             var link = $( this ).attr("href") );
             alert(link );
         });
    });
</script>

You can also get all links using this one also (javascript)

 var list = document.getElementsByTagName("a");

Not the answer you're looking for? Browse other questions tagged or ask your own question.