file_get_contents 采集网页内容实现自定义函数过滤

本文介绍了一种使用PHP实现的网页内容抓取方法,包括如何通过函数抓取指定字符串之间的内容,并进行编码自动转换为UTF-8,确保跨平台的兼容性。此外,还展示了如何利用正则表达式去除HTML标签。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以下两个函数是实现字符任意攫取和编码自动转换成utf8,可以不用考虑编码
function strCutByStr($str,$start,$end)          
{  
$temp=strstr($str,$start);
$temp=str_replace($start,'',$temp);
$contgzw=strstr($temp,$end,TRUE);
 return $contgzw;         
} 

function array_iconv($data, $output = 'utf-8') {
  $encode_arr = array('UTF-8','ASCII','GBK','GB2312','BIG5','JIS','eucjp-win','sjis-win','EUC-JP');
  $encoded = mb_detect_encoding($data, $encode_arr);
  if (!is_array($data)) {
    return mb_convert_encoding($data, $output, $encoded);
  }
  else {
    foreach ($data as $key=>$val) {
      $key = array_iconv($key, $output);
      if(is_array($val)) {
        $data[$key] = array_iconv($val, $output);
      } else {
      $data[$key] = mb_convert_encoding($data, $output, $encoded);
      }
    }
  return $data;
  }
}

调用$fcontents =array_iconv(file_get_contents($url), $output = 'utf-8');

智库ams新闻采集系统使用的采集过程代码

$str =array_iconv(file_get_contents('https://www.chumoshu.com'), $output = 'utf-8');

$title= strCutByStr($str,$zw7428cn_titlestart,$zw7428cn_titleend);
$content= trim(strCutByStr($str, $zw7428cn_contentstart, $zw7428cn_contentend));

$content = preg_replace("/<a[^>]*>/i", "", $content);  
   $content = preg_replace("/<\/a>/i", "", $content);   
   $content = preg_replace("/<div[^>]*>/i", "", $content);  
   $content = preg_replace("/<\/div>/i", "", $content);           
   $content = preg_replace("/<!--[^>]*-->/i", "", $content);         
   $content = preg_replace("/style=.+?['|\"]/i",'',$content); 
   $content = preg_replace("/class=.+?['|\"]/i",'',$content); 
   $content = preg_replace("/id=.+?['|\"]/i",'',$content);    
   $content = preg_replace("/lang=.+?['|\"]/i",'',$content);    
   $content = preg_replace("/width=.+?['|\"]/i",'',$content);   
   $content = preg_replace("/height=.+?['|\"]/i",'',$content);  
   $content = preg_replace("/border=.+?['|\"]/i",'',$content);  
   $content = preg_replace("/face=.+?['|\"]/i",'',$content);  
   $content = preg_replace("/<script[\s\S]*?<\/script>/i",'',$content);  
   $content = preg_replace("/face=.+?['|\"]/",'',$content);
   $content =trim($content);
echo '<font color=red>成功采集文件:</font>'.$title;
$zw7428cn_bookid=$zw7428cn_mulu;
$zw7428cn_bookname=$zw7428cn_bookname;

	 $zw7428cn_articlename=addslashes(htmlspecialchars($title));
	 $zw7428cn_content=addslashes(htmlspecialchars($content));
$zw7428cn_date=date('y-m-d h:i:s',time());
	 $sql="insert into zw7428cnchapter(zw7428cn_bookid,zw7428cn_bookname,zw7428cn_articlename,zw7428cn_content,zw7428cn_date) values('$zw7428cn_bookid','$zw7428cn_bookname','$zw7428cn_articlename','$zw7428cn_content','$zw7428cn_date')";
	 mysqli_query($link,$sql);
echo '<font color=red>同时已成功入库!</font><br>';

以上是采集触摸书城的主要实现代码部分以及如何入库部分。

来源:未来智库 https://www.7428.cn


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值