PHP 采集获取指定网址的内容_PHP教程
程序员文章站
2024-04-05 14:36:48
...
参考别人想法变成自己的想法,你会发现慢慢下来以后你就拥有了临时解决很多问题的思路与方法。
/*
功能:获取页面内容,存储下来阅读; lost63
*/
Class GetUrl{
var $url; //地址
var $result; //结果
var $content; //内容
var $list; //列表
function GetUrl($url){
$this->url=$url;
$this->GetContent();
$this->GetList();
$this->FileSave();
//print_r($this->list[2]);
}
private function GetContent(){
$this->result=fopen($this->url,"r");
while(!feof($this->result)){
$this->content.=fgets($this->result,9999);
}
}
private function GetList(){
preg_match_all('/(.*?)/',$this->content,$this->list);
$this->list[2]=array_unique($this->list[2]); //移除相同的值
while(list($key,$value)=each($this->list[2])){
if(strpos($value,".html")==0||strpos($value,"jiaocheng")==0){
unset($this->list[2][$key]);
}else{
$this->list[2][$key]=substr($value,0,strpos($value,".html")).".html"; //去掉不需要的标签
}
}
}
private function FileSave(){
foreach($this->list[2] as $value){
$this->url=$value; //重新赋值
$this->content=null;
$this->GetContent(); //提取内容
preg_match_all('/(.*?)/',$this->content,$files); //取标题
$filename=$files[1][0].".html"; //存储名
$content=$this->str_cut($this->content,'http://pagead2.googlesyndication.com/pagead/show_ads.js','
复制代码 代码如下:
/*
功能:获取页面内容,存储下来阅读; lost63
*/
Class GetUrl{
var $url; //地址
var $result; //结果
var $content; //内容
var $list; //列表
function GetUrl($url){
$this->url=$url;
$this->GetContent();
$this->GetList();
$this->FileSave();
//print_r($this->list[2]);
}
private function GetContent(){
$this->result=fopen($this->url,"r");
while(!feof($this->result)){
$this->content.=fgets($this->result,9999);
}
}
private function GetList(){
preg_match_all('/(.*?)/',$this->content,$this->list);
$this->list[2]=array_unique($this->list[2]); //移除相同的值
while(list($key,$value)=each($this->list[2])){
if(strpos($value,".html")==0||strpos($value,"jiaocheng")==0){
unset($this->list[2][$key]);
}else{
$this->list[2][$key]=substr($value,0,strpos($value,".html")).".html"; //去掉不需要的标签
}
}
}
private function FileSave(){
foreach($this->list[2] as $value){
$this->url=$value; //重新赋值
$this->content=null;
$this->GetContent(); //提取内容
preg_match_all('/
$filename=$files[1][0].".html"; //存储名
$content=$this->str_cut($this->content,'http://pagead2.googlesyndication.com/pagead/show_ads.js','
');
$file=fopen($filename,"w");
fwrite($file,$content);
fclose($file);
echo $filename."保存 OK
\n";
}
}
function str_cut($str ,$start, $end) {
$content = strstr( $str, $start );
$content = substr( $content, strlen( $start ), strpos( $content, $end ) - strlen( $start ) );
return $content;
}
}
$w=new GetUrl("http://www.ijavascript.cn/jiaocheng/javascript-jiaocheng-352.html");
?>
$file=fopen($filename,"w");
fwrite($file,$content);
fclose($file);
echo $filename."保存 OK
\n";
}
}
function str_cut($str ,$start, $end) {
$content = strstr( $str, $start );
$content = substr( $content, strlen( $start ), strpos( $content, $end ) - strlen( $start ) );
return $content;
}
}
$w=new GetUrl("http://www.ijavascript.cn/jiaocheng/javascript-jiaocheng-352.html");
?>
推荐阅读
-
PHP 采集获取指定网址的内容_PHP教程
-
PHP获取mysql数据表的字段名称和详细信息的方法,mysql字段_PHP教程
-
PHP 采集获取指定网址的内容_php技巧
-
PHP获取汉字的拼音(全部与首字母)(转载),php汉字_PHP教程
-
PHP获取PR值的代码_PHP教程
-
PHP获取当前完整URL地址的函数,php获取当前url_PHP教程
-
php简单实现屏蔽指定ip段用户的访问,phpip_PHP教程
-
PHP函数ini_get_all获取设置选项变量的方法介绍_PHP教程
-
在字符串指定位置插入一段字符串的php代码_PHP教程
-
php使用CURL不依赖COOKIEJAR获取COOKIE的方法,curlcookiejar_PHP教程