如何用PHP编写代码解析网页并自动下载其中的图片到本地?

2026-04-05 16:351阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计121个文字,预计阅读时间需要1分钟。

如何用PHP编写代码解析网页并自动下载其中的图片到本地?

php

如何用PHP编写代码解析网页并自动下载其中的图片到本地?

<?php //error_reporting(0);//关闭错误报告 include_once("simple_html_dom.php");//引入解析库 $url='www.w3cschool.cc/mongodb/mongodb-window-install.html'; $html=file_get_html($url); while($url[strlen($url)-1]!='/')$url=substr($url,0,-1);//获得上层目录 if(!is_dir($filepath)){mkdir($_SERVER['DOCUMENT_ROOT']."/imgtmp/",1);}//创建目录 foreach($html->find('img') as $im) { $p=$im->src; if($p[0]=='/'||$p[0]=='\\')$p=$url."..".$p;//补全url file_put_contents("imgtmp/".md5(microtime()).".".substr($p,-3),file_get_contents($p)); } ?>

本文共计121个文字,预计阅读时间需要1分钟。

如何用PHP编写代码解析网页并自动下载其中的图片到本地?

php

如何用PHP编写代码解析网页并自动下载其中的图片到本地?

<?php //error_reporting(0);//关闭错误报告 include_once("simple_html_dom.php");//引入解析库 $url='www.w3cschool.cc/mongodb/mongodb-window-install.html'; $html=file_get_html($url); while($url[strlen($url)-1]!='/')$url=substr($url,0,-1);//获得上层目录 if(!is_dir($filepath)){mkdir($_SERVER['DOCUMENT_ROOT']."/imgtmp/",1);}//创建目录 foreach($html->find('img') as $im) { $p=$im->src; if($p[0]=='/'||$p[0]=='\\')$p=$url."..".$p;//补全url file_put_contents("imgtmp/".md5(microtime()).".".substr($p,-3),file_get_contents($p)); } ?>