本文实例讲述了PHP采集静态页面并把页面css,img,js保存的方法。分享给大家供大家参考。具体分析如下:
这是一个可以获取网页的html代码以及css,js,font和img资源的小工具,主要用来快速获取模板,如果你来不及设计UI或者看到不错的模板,则可以使用这个工具来抓取网页和提取资源文件,提取的内容会按相对路径来保存资源,因此你不必担心资源文件的错误url导入.
首页 index.php,代码如下:
Web Grabber
Url
Save All
List
抓取页面代码 grab.php,代码如下:
* flute
* 2014/03/31
*/
if(isset($_POST['url'])) {
if(isset($_POST['project']) && !is_dir($_POST['project'])) mkdir($_POST['project'], 0777);
echo json_encode(grab($_POST['url']));
}
function grab($url) {
//$url = 'http://ldixing-wordpress.stor.sinaapp.com/uploads/leaves/test.html';
$data = array();
$file = preg_replace('/^.*//', '', $url);
if(($content = file_get_contents($url)) !== false) {
if(isset($_POST['project'])) file_put_contents($_POST['project'].'/'.$file, $content);
$pattern = '/
if(preg_match_all($pattern, $content, $matches)) {
$data['css'] = $matches[2];
}
$pattern = '/
if(preg_match_all($pattern, $content, $matches)) {
$data['js'] = $matches[2];
}
$pattern = '/
if(preg_match_all($pattern, $content, $matches)) {
$data['img'] = $matches[2];
}
$pattern = '/url(('|"|s)(.*?)1)/i';
if(preg_match_all($pattern, $content, $matches)) {
$data['src'] = $matches[2];
}
}
return $data;
}
function vardump($obj) {
echo '
'; <br> print_r($obj); <br> echo '';
}
?>
保存css,js,img等资源的页面 save.php,代码如下:
* flute
* 2014/03/31
*/
if(isset($_POST['url']) && isset($_POST['project']) && isset($_POST['domain'])) {
extract($_POST);
$url = preg_replace('/?.*$/', '', $url);
$file = $url;
$arr = explode('/', $file);
$length = sizeof($arr);
$filename = $arr[$length - 1];
$root = $project;
$dir = $root;
if($domain == 'http') {
$dir = $root.'/http';
if(!is_dir($dir)) mkdir($dir, 0777);
} else {
$file = $domain.'/'.$url;
for($i = 0; $i if(!emptyempty($arr[$i])) {
$dir .= '/'.$arr[$i];
if(!is_dir($dir)) mkdir($dir, 0777);
}
}
}
if(!file_exists($dir.'/'.$filename) || filesize($dir.'/'.$filename) == 0) {
$content = file_get_contents($file);
file_put_contents($dir.'/'.$filename, $content);
}
}
?>
使用方法:
1. 打开index页,输入项目名和要抓取的网址,网址必须是文件名结尾,如index.html;
2. 点Get按钮,得到当前页面所有的css,js,img等资源列表;
3. 点击css链接会获取css文件中的背景资源图片,附加在列表后头;
4. 点击Save All即可保存列表中所有的文件,并按相对路径生成;
5. 如果网页上有http远程文件,将会直接保存在http文件夹下;
6. Get和Save有时会失败,没关系重试几次即可。
希望本文所述对大家的php程序设计有所帮助。

tomakephpapplicationsfaster,關注台詞:1)useopcodeCachingLikeLikeLikeLikeLikePachetoStorePreciledScompiledScriptbyTecode.2)MinimimiedAtabaseSqueriSegrieSqueriSegeriSybysequeryCachingandeffeftExting.3)Leveragephp7 leveragephp7 leveragephp7 leveragephpphp7功能forbettercodeefficy.4)

到ImprovephPapplicationspeed,關注台詞:1)啟用opcodeCachingwithapCutoredUcescriptexecutiontime.2)實現databasequerycachingingusingpdotominiminimizedatabasehits.3)usehttp/2tomultiplexrequlexrequestsandreduceconnection.4 limitesclection.4.4

依赖注入(DI)通过显式传递依赖关系,显著提升了PHP代码的可测试性。1)DI解耦类与具体实现,使测试和维护更灵活。2)三种类型中,构造函数注入明确表达依赖,保持状态一致。3)使用DI容器管理复杂依赖,提升代码质量和开发效率。

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

phpisusedforsenderemailsduetoitsbuilt-inmail()函數andsupportivelibrariesLikePhpMailerAndSwiftMailer.1)usethemail()functionForbasiceMails,butithasimails.2)butithasimail.2)

PHP性能瓶颈可以通过以下步骤解决:1)使用Xdebug或Blackfire进行性能分析,找出问题所在;2)优化数据库查询并使用缓存,如APCu;3)使用array_filter等高效函数优化数组操作;4)配置OPcache进行字节码缓存;5)优化前端,如减少HTTP请求和优化图片;6)持续监控和优化性能。通过这些方法,可以显著提升PHP应用的性能。

依賴性注射(DI)InphpisadesignPatternthatManages和ReducesClassDeptions,增強量強制性,可驗證性和MATIALWINABIOS.ItallowSpasspassingDepentenciesLikEdenciesLikedAbaseConnectionStoclasseconnectionStoclasseSasasasasareTers,interitationAseTestingEaseTestingEaseTestingEaseTestingEasingAndScalability。

cachingimprovesphpermenceByStorcyResultSofComputationsorqucrouctationsorquctationsorquickretrieval,reducingServerLoadAndenHancingResponsetimes.feftectivestrategiesinclude:1)opcodecaching,whereStoresCompiledSinmememorytssinmemorytoskipcompliation; 2)datacaching datacachingsingMemccachingmcachingmcachings


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中