找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1710|回复: 1
打印 上一主题 下一主题

基于Snoopy的PHP近似完美获取网站编码的代码

[复制链接]

2600

主题

2600

帖子

7736

积分

论坛元老

Rank: 8Rank: 8

积分
7736
跳转到指定楼层
楼主
发表于 2018-2-14 08:32:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            先要到网上下载Snoopy.class.php
调用方法:
[U]复制代码[/U] 代码如下:
getCharset();
?>

[U]复制代码[/U] 代码如下:
url=$url;
}
//打开网站
private function open($url)
{
if($this->request!==null)
{
if($this->request->status==200)
{
return true;
}
else
{
return false;
}
}
else
{
$this->request=new Snoopy();
$this->request->fetch($url);
if($this->request->status==200)
{
$this->request->results=strtolower($this->request->results);
$charset=$this->getCharset();
if($charset!="utf-8")
{
if($charset=="windows-1252")
{
$this->request->results=$this->uni_decode($this->request->results);
}
else
{
$this->request->results=mb_convert_encoding($this->request->results,"UTF-8",$charset);
}
}
return true;
}
else
{
return false;
}
}
}
//获取网站title,keywords,description
public function getWebinfo()
{
$info=array(
'title'=>'',
'keywords'=>'',
'desc'=>'',
'ip'=>''
);
if(!$this->open($this->url)){return $info;exit;}
// print_r($this->request->results);exit;
preg_match('/([^>]*)/si', $this->request->results, $titlematch );
if (isset($titlematch) && is_array($titlematch) && count($titlematch) > 0)
{
$info['title'] = strip_tags($titlematch[1]);
}
preg_match_all('/"]*)"?[\s]*' . 'content="?([^>"]*)"?[\s]*[\/]?[\s]*>/si', $this->request->results, $match);
$ft=0;
foreach($match[1] as $mt)
{
if($mt=="keywords" || $mt=="description")
{
$ft=1;
}
}
if($ft==0)
{
preg_match_all('/"]*)"?[\s]*name="?' . '([^>"]*)"?[\s]*[\/]?[\s]*>/si', $this->request->results, $match);
if (isset($match) && is_array($match) && count($match) == 3)
{
$originals = $match[0];
$names = $match[2];
$values = $match[1];
if (count($originals) == count($names) && count($names) == count($values))
{
$metaTags = array();
for ($i=0, $limiti=count($names); $i  htmlentities($originals[$i]),
'value' => $values[$i]
);
}
}
}
}
else
{
if (isset($match) && is_array($match) && count($match) == 3)
{
$originals = $match[0];
$names = $match[1];
$values = $match[2];
if (count($originals) == count($names) && count($names) == count($values))
{
$metaTags = array();
for ($i=0, $limiti=count($names); $i  htmlentities($originals[$i]),
'value' => $values[$i]
);
}
}
}
}
$result = array (
'metaTags' => $metaTags
);
if(isset($result['metaTags']['keywords']['value']))
{
$info['keywords']=$result['metaTags']['keywords']['value'];
}
else
{
$info['keywords']="";
}
if(isset($result['metaTags']['description']['value']))
{
$info['desc']=$result['metaTags']['description']['value'];
}
else
{
$info['desc']="";
}
$domain=preg_replace('/http\:\/\//si', '', $this->url);
$ip=@gethostbyname($domain);
$ip_arr=explode(".", $ip);
if(count($ip_arr)==4)
{
$info['ip']=$ip;
}
return $info;
}
public function t($string,$o)
{
for($i=0;$iopen($this->url)){return false;exit;}
//首先从html获取编码
preg_match("/request->results,$temp) ? strtolower($temp[1]):"";
if($temp[1]!="")
{
if(in_array($temp[1], $this->charset_arr))
{
if($temp[1]=="gb2312")
{
$tmp_charset=$this->t($this->request->results,$temp[1]);
if($tmp_charset==$temp[1])
{
return $temp[1];
}
}
else
{
return $temp[1];
}
}
}
if(!empty($this->request->headers))
{
//从header中获取编码
$hstr=strtolower(implode("
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

2600

主题

2600

帖子

7736

积分

论坛元老

Rank: 8Rank: 8

积分
7736
沙发
发表于 2018-2-14 08:32:24 | 只看该作者
",$this->request->headers));
preg_match("/charset=[^\w]?([-\w]+)/is",$hstr,$lang) ? strtolower($lang[1]):"";
if($lang[1]!="")
{
return $lang[1];
}
}
$encode_arr=array("UTF-8","GB2312","GBK","BIG5","ASCII","EUC-JP","Shift_JIS","CP936","ISO-8859-1","JIS","eucjp-win","sjis-win");
$encoded=mb_detect_encoding($this->request->results,$encode_arr);
if($encoded)
{
return strtolower($encoded);
}
else
{
return false;
}
}
}
?>
            
            
您可能感兴趣的文章:
  • snoopy PHP版的网络客户端提供本地下载
  • snoopy 强大的PHP采集类使用实例代码
  • PHP采集利器 Snoopy 试用心得
  • php使用curl和正则表达式抓取网页数据示例
  • PHP采集类Snoopy抓取图片实例
  • PHP采集类snoopy详细介绍(snoopy使用教程)
  • php结合正则批量抓取网页中邮箱地址
  • php中Snoopy类用法实例
  • php基于Snoopy解析网页html的方法
  • PHP正则表达式抓取某个标签的特定属性值的方法
  • PHP怎样用正则抓取页面中的网址
  • PHP正则+Snoopy抓取框架实现的抓取淘宝店信誉功能实例
            
  • 回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端