扒网站当中一般css中的图片扒不下来,这个脚本就是用来下载这些图片到本地的
流程
1、获取css文件路径
2、打开文件逐行读取判断是否包含需要的图片
2.1 包含则 -进行截取直接获取到相对路径
2.1.1 根据curl来文件流,fopne打开文件,fwrite写入文件
2.2不包含则跳出
<?php
/**
* Created by PhpStorm.
* User: 小狗蛋儿
* Date: 2018/3/19
* Time: 14:39
*
* 获取css文件下面所有的css文件
* 然后替换所有的background
*/ $dirname=dirname("D:\\ibwant\\new.css-13.css"); $root_url="xxxxxx"; //网站根目录 $rege="";
if (!is_dir($dirname)){
echo "不是目录".$dirname;
}else { $file=array(); $scandir = scandir($dirname);
foreach ($scandir as $value){
if (getExtends($value)=="css"){ $handle = fopen ($dirname."\\".$value, "r");
GetsSave($handle,$value); }else{
continue;
} }
} /**
* @param $files
* @return mixed
* 出现的位置
*/
function getExtends($files){
return pathinfo($files, PATHINFO_EXTENSION);
} /**
* @param $opens
* @param $names
* 获取指定文件的位置
*/
function GetsSave($opens,$names){
$fl=array();
//feof 判断检测文件是否到末尾
while(!feof($opens)){
$contents=fgets($opens);
if(strstr($contents,"/*tpa=")){
$fl[]=substr($contents,strpos($contents,"=")+1); }else{
continue;
}
}
getDows($fl);
fclose($opens);
} // $s="xxxx";
// echo strpos($s,"*"); function getDows($as){
$cas=array();
foreach ($as as $value){ if (substr($value,0,4)!="http"){
continue;
}else {
$nums = strpos($value, "*");
$cas[] = substr($value, 0, $nums);
}
}
Saveimg($cas); } function Saveimg($ims){
foreach ($ims as $values){
$contens=trim(strrchr($values,"/"),"/"); // $filenames=substr($contens,0,strpos($contens,"."));
$return_content = http_get_data($values);
//var_dump($return_content); $fp= @fopen($contens,"a");
fwrite($fp,$return_content); } } function http_get_data($url){
$ch=curl_init();
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, 'GET' );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt ( $ch, CURLOPT_URL, $url );
ob_start ();
curl_exec ( $ch );
$return_content = ob_get_contents ();
ob_end_clean (); $return_code = curl_getinfo ( $ch, CURLINFO_HTTP_CODE );
return $return_content;
}