天天看點

php 抓取網站圖檔的簡單程式

php下載下傳css檔案中的圖檔

php 抓取網站圖檔的簡單程式

<?  

function getimagesfromcssfile() {  

//note 設定php逾時時間  

    set_time_limit(0);  

//note 取得樣式檔案内容  

    $stylefilecontent = file_get_contents('images/style.css');  

//note 比對出需要下載下傳的url位址  

    preg_match_all("/url\(.+?\)/", $stylefilecontent, $imagesurlarray);  

//note 循環需要下載下傳的位址,逐個下載下傳  

    $imagesurlarray = array_unique($imagesurlarray[0]);  

    foreach ($imagesurlarray as $imagesurl) {  

        $imagesurl = str_ireplace(array("url(", ")", "'",'"'), '', $imagesurl);  //url(" ") url('')  

        if (preg_match('/^http.*/', $imagesurl)) {   //跳過網絡圖檔  

            continue;  

        }  

        file_put_contents(basename($imagesurl), file_get_contents($imagesurl));  

    }  

}  

php 抓取網站圖檔的簡單程式

<?php  

/*完成網頁内容捕獲功能*/  

function get_img_url($site_name) {  

    $site_fd = fopen($site_name, "r");  

    $site_content = "";  

    while (!feof($site_fd)) {  

        $site_content .= fread($site_fd, 1024);  

    /*利用正規表達式得到圖檔連結*/  

    $reg_tag = '/<img.*?\"([^\"]*(jpg|bmp|jpeg|gif)).*?>/';  

    $ret = preg_match_all($reg_tag, $site_content, $match_result);  

    fclose($site_fd);  

    return $match_result[1];  

/* 對圖檔連結進行修正 */  

function revise_site($site_list, $base_site) {  

    foreach ($site_list as $site_item) {  

        if (preg_match('/^http/', $site_item)) {  

            $return_list[] = $site_item;  

        } else {  

            $return_list[] = $base_site . "/" . $site_item;  

    return $return_list;  

/*得到圖檔名字,并将其儲存在指定位置*/  

function get_pic_file($pic_url_array, $pos) {  

    $reg_tag = '/.*\/(.*?)$/';  

    $count = 0;  

    foreach ($pic_url_array as $pic_item) {  

        $ret = preg_match_all($reg_tag, $pic_item, $t_pic_name);  

        $pic_name = $pos . $t_pic_name[1][0];  

        $pic_url = $pic_item;  

        print("downloading " . $pic_url . "\n");  

        $img_read_fd = fopen($pic_url, "r");  

        $img_write_fd = fopen($pic_name, "w");  

        $img_content = "";  

        while (!feof($img_read_fd)) {  

            $img_content .= fread($img_read_fd, 1024);  

        fwrite($img_write_fd, $img_content);  

        fclose($img_read_fd);  

        fclose($img_write_fd);  

        print("[ok]\n");  

    return 0;  

function main() {  

    /* 待抓取圖檔的網頁位址 */  

    $site_name = "http://image.cn.yahoo.com";  

    $img_url = get_img_url($site_name);  

    $img_url_revised = revise_site($img_url, $site_name);  

    $img_url_unique = array_unique($img_url_revised); //unique array  

    get_pic_file($img_url_unique, "./");  

main();  

?>  

此程式略有不足,如果圖檔在網站伺服器上不同目次下但檔案名是相同的,此時圖檔有可能是不一樣的,但在最後生存時,後面得到的圖檔會将前邊已生存的圖檔覆 蓋掉,如在http://example.com/網站上有圖檔連結http://example.com/pic/test1.jpg和http: //example.com/pic/new/test1.jpg那麼在下載下傳時這兩張圖檔隻有一張生存,另外一張就被覆寫掉,修正的方法是在每派生的存前 先檢索目前目次下是否已有此檔案名,有的話對将派生的存的圖檔從頭命名即可。

php 抓取網站圖檔的簡單程式

//取得頁面所有的圖檔位址  

function getimages($str){  

    $match_str = "/((http://)+([^ rn()^$!`"'|[]{}<>]*)((.gif)|(.jpg)|(.bmp)|(.png)|(.gif)|(.jpg)|(.png)|(.bmp)))/";  

    preg_match_all ($match_str,$str,$out,preg_pattern_order);  

    return $out;  

}?>