Created
April 14, 2018 19:36
-
-
Save abhij89/6adc9774f774b4dc29b3d7029f3053b8 to your computer and use it in GitHub Desktop.
Quick php code for scrapping images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// For no time limit | |
set_time_limit(0); | |
$url = "url-to-external-page"; | |
$pagecontent = file_get_contents($url); | |
$dom = new DOMDocument(); | |
$dom->loadHTML($pagecontent); | |
// find image by class name | |
$finder = new DomXPath($dom); | |
$classname = "img-fluid"; // eg class name - img-fluid | |
$nodes = $finder->query("//*[contains(@class, '$classname')]"); | |
// or use below to find by element | |
// $nodes = $dom->getElementsByTagName('img'); | |
foreach ($nodes as $picture) { | |
foreach ($picture->attributes as $attr) { | |
if ($attr->nodeName == "src") { | |
$img = 'path-to-local-path-to-save-image'; | |
file_put_contents($img, file_get_contents($attr->nodeValue)); | |
} else { | |
continue; | |
} | |
} | |
$count++; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment