看到一个喜欢的网站,想抓点图片素材,一个一个复制盛世费劲,分析CSS发现图片元素都写在CSS里面。就不要怪我了。
帖代码
把css内容复制到到 $str中 或者 file_get_contents CSS的路径 也可以 $str='@charset "utf-8"; body { width: 100%; height: 100%; background: url(../images/bg.jpg) no-repeat; ....... ';
正则匹配
preg_match_all('/url\((.*?)\)/u',$str,$match);
循环抓取
foreach ($match[1] as $key => $value) { $value=str_replace('../','对方网站url/',$value); $base = basename($value); $urlds1=file_get_contents($value); $nweimg1='images/'.$base; write_file($nweimg1,$urlds1); }
中间用到的方法write_file
function write_file($filename,$data,$method="rb+",$iflock=1){ @touch($filename); $handle=@fopen($filename,$method); if($iflock){ @flock($handle,LOCK_EX); } @fputs($handle,$data); if($method=="rb+") @ftruncate($handle,strlen($data)); @fclose($handle); @chmod($filename,0777); if( is_writable($filename) ){ return 1; }else{ return 0; } }