PHP Image Scraper仅选择图像

时间:2015-02-16 09:51:23

标签: php dom

我正在尝试构建一个简单的图像处理程序来从网站中删除某些图像,但到目前为止我所拥有的所有图像都已擦除所有图像

<?php
$url = "http://www.techbuy.com.au/";
$html = file_get_contents('http://www.techbuy.com.au/');
$dom = new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
foreach ($images as $image)
{
$fimage =  $image->getAttribute('src');
echo "<img src='$url" . "$fimage' ></img>";
}
?>

如何让它说废弃第二张图片并留下其余图片

2 个答案:

答案 0 :(得分:2)

if ($images->length >= 2) { $src = $images->item(1)->getAttribute("src"); }

答案 1 :(得分:2)

<?php
$url = "http://www.techbuy.com.au/";
$html = file_get_contents('http://www.techbuy.com.au/');
$dom = new domDocument;
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');

foreach ($images as $key => $image)
{
    if ($key === 1) {
        $fimage =  $image->getAttribute('src');
        echo "<img src='$url" . "$fimage' ></img>";
    }
}
?>

抓住第二张图片。