2015-01-05 35 views
0

我如何打開一個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"; 
} 
+0

你需要一個HTML解析器,像simplehtmldom.sourceforge.net或php.net/manual/en/domdocument.loadhtml.php – Eugen

回答

-1

我建議你使用wget的如果你想與所有抓取網頁它的內容(圖片,JS,CSS等)。

$your_url = "http://www.google.com/information.php"; 
$your_output_dir = "/whatever/dir/you/might/use/"; 
$you_logs = "/your/log/dir/wget.log"; 
$cmd = "wget -p --convert-links $your_url -P $your_output_dir -o $you_logs"; 
exec($cmd); 

做檢查wget的手冊頁幫助,或者谷歌搜索wget的例子

+0

因此,在這個例子中它會只保存information.php頁面的圖片?或者它只記錄圖像鏈接? – user3502178

+0

它會保存網頁和網頁所使用的所有資源(圖像,css,js等) – rtome

+0

並將其保存到您指定的內容中$ your_output_dir – rtome

0

嘗試使用「Simple HTML DOM Parser」庫(http://simplehtmldom.sourceforge.net/)之類的東西。

你的代碼可能看起來是這樣的:

<?php 
include('simple_html_dom.php'); 
$URL = "http://www.google.com/information.php"; 
$dumpDir = "dumpDir/"; 

//Get the page as a whole  
$html = file_get_html($URL); 

//Find all the images located within div 
foreach($html->find("div#displayimage img") as $img){ 
    $src = $img->src; 

    //Get filename 
    $filename = substr($img->src, strrpos($img->src, "/")+1); 

    //Quick fix for relative file paths 
    if (strtolower(substr($src, 0, 5)) != 'http:' && strtolower(substr($src, 0, 6)) != 'https:') $src = $URL.$src; 

    // Save the file 
    file_put_contents($dumpDir.$filename, file_get_contents($src)); 
} 
?> 
相關問題