Web Scraping how to insert result within img src =

0

I'm doing a web scraping of a website, but I'd like the returned images to already come to me within <img src= but I'm not succeeding

// Find all images 
foreach($html->find('img') as $element) 
       echo $element->src . '<br>';

I tried this here for example but it did not work

<img src".$element->src .."> '<br>';
    
asked by anonymous 22.02.2017 / 04:13

2 answers

3

The correct syntax, if it is in html is:

<img src="<?php echo $element->src;?>"/> 
    
22.02.2017 / 04:59
3

You can use XPath for this and use getAttribute .

// Inicia o DOM:
$html = $retorno_do_seu_curl;

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

// Inicia XPath:
$xpath = new DomXPath($dom);

// Encontra todos os '<img>'.
$imagens = $xpath->query('//img');

// Faz um loop para cada imagem obtida:
foreach($imagens as $_imagem){

    // Obtem o 'src' da respetiva imagem:
    echo '<img src="' . $_imagem->getAttribute('src') . '">';

}

Test this by clicking here.

If you do not want to use XPath just use getElementsByTagName then getAttribute .

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

$imagens = $dom->getElementsByTagName('img');

foreach($imagens as $_imagem){

    echo '<img src="' . $_imagem->getAttribute('src') . '">';

}
    
22.02.2017 / 06:23