1

我正在使用查询图像getElementsByTagName("img")并使用它打印它image->src,它不起作用。我也试过用image->nodeValue这个不起作用。

require('simple_html_dom.php');

$dom=new DOMDocument();
$dom->loadHTML( $str);       /*$str contains html output */

$xpath=new DOMXPath($dom); 
$imgfind=$dom->getElementsByTagName('img');  /*finding elements by tag name img*/

foreach($imgfind as $im)
{
    echo $im->src;        /*this doesnt work  */   
    /*echo $im->nodeValue;  and also this doesnt work (i tried both of them             separately ,Neither of them worked)*/

    // echo "<img src=".$im->nodeValue."</img><br>"; //This also did not work
}

/*the image is encolsed within div tags.so i tried to query value of div and print but still image was not printed*/

$printimage=$xpath->query('//div[@class="abc"]'); 
foreach($printimage as $image)
{
    echo $image->src;   //still i could not accomplish my task
}
4

2 回答 2

6

好的,使用它来显示您的图像:

foreach($imgfind as $im)
{
  echo "<img src=".$im->getAttribute('src')."/>"; //use this instead of echo $im->src;
}

它肯定会显示您的图像。确保图像的路径正确

于 2013-04-17T08:50:57.503 回答
0

埃斯佩罗特西瓦

  $dom = new DOMDocument();
  $filename = "https://www.amazon.com/dp/B0896WB9XD/";
  $html = file_get_contents($filename);

  @$dom->loadHTML($html);

$imgfind=$dom->getElementsByTagName('img');
foreach($imgfind as $im)
{
    $ids= $im->getAttribute('id'); 

    if ($ids == 'landingImage') {
        $im2 = $im->getAttribute('src'); 
       echo '<img src="'.$im2.'">';
    }
    else{
        
    }
}

对亚马逊。

于 2021-09-17T05:11:48.520 回答