0

I want to retrieve the alt from an image on a website. In the past, I have done it like this:

@$doc=new DOMDocument();

@$doc->loadHTML($html);  //$html is the website

 $xml=simplexml_import_dom($doc); // just to make xpath more simple

 $images=$xml->xpath('//img');
   foreach ($images as $img) {                                  
   echo  $img['alt'];

   }

but now the image I want is within div style="padding: 15px 0px 15px 25px;"

How would I go about retrieving the image? I have tried to change the $xml->xpath() to something else but no luck.

If anyone knows what I need to do, it would be much appreciated.

Thanks!

My mistake, this is actually where the data is contained.

 <div class="numbers">
 <table cellpadding="3">
  <tbody>
  <tr>
  <tr>
  <td>
  <img alt="12" src="/images/new/date/date5.gif">
   <img alt="39" src="/images/new/date/date4.gif">
   <img alt="32" src="/images/new/date/date3..gif">
   <img alt="4" src="/images/new/date/date2.gif">
   <img alt="20" src="/images/new/date/date1.gif">
   </td>
   <td>
   </tr>
   </tbody>
   </table>
    </div>
Teddy13
  • 3,824
  • 11
  • 42
  • 69

2 Answers2

0

Try this XPath:

//div[@style="padding: 15px 0px 15px 25px;"]/img

But be careful that the style is exactly like this in the HTML source and remember that browsers can change the HTML/CSS code you see when you look at the source! So use the Firefox Web Developer toolbar with the option show "Source"! Check this topic https://stackoverflow.com/a/3314453/22470

Community
  • 1
  • 1
powtac
  • 40,542
  • 28
  • 115
  • 170
0

if it is inside a div then add the div with the path

  $doc=new DOMDocument();
    $doc->loadHTML("<html><body>Test<br><img src=\"myimage.jpg\" title=\"title\" alt=\"alt\"></body></html>");
    $xml=simplexml_import_dom($doc); // just to make xpath more simple
    $images=$xml->xpath('//div[@style="padding: 15px 0px 15px 25px;"]/img');
    foreach ($images as $img) {
        echo $img['src'] . ' ' . $img['alt'] . ' ' . $img['title'];
    }
Manigandan Arjunan
  • 2,260
  • 1
  • 25
  • 42