我如何打開一個URL例如:http://www.google.com/information.php,然後保存顯示在information.php文件中的所有圖像,並且只能在div標籤之間保存圖像「displayimg」如何打開一個URL並保存頁面上的所有圖像
如果你幫我我會很高興!我所知道的是,我可以使用cURL,但不知道如何在這些請求後生成。
謝謝!
function getimg($url) {
$headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';
$headers[] = 'Connection: Keep-Alive';
$headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';
$user_agent = 'php';
$process = curl_init($url);
curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
curl_setopt($process, CURLOPT_HEADER, 0);
curl_setopt($process, CURLOPT_USERAGENT, $useragent);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
$return = curl_exec($process);
curl_close($process);
return $return;
}
$imgurl = 'http://www.foodtest.ru/images/big_img/sausage_3.jpg';
$imagename= basename($imgurl);
if(file_exists('./tmp/'.$imagename)){continue;}
$image = getimg($imgurl);
file_put_contents('tmp/'.$imagename,$image);
編輯:使用
林這段代碼了,但我怎麼能連接存儲陣列中的左右能夠將圖像下載到我的服務器?
require_once('simplehtmldom/simple_html_dom.php');
require_once('url_to_absolute.php');
$url = 'http://www.electrictoolbox.com/php-get-meta-tags-html-file/';
$html = file_get_html($url);
foreach($html->find('img') as $element) {
echo url_to_absolute($url, $element->src), "\n";
}
你需要一個HTML解析器,像simplehtmldom.sourceforge.net或php.net/manual/en/domdocument.loadhtml.php – Eugen