首頁  >  文章  >  後端開發  >  基於Snoopy的PHP近似完美取得網站編碼

基於Snoopy的PHP近似完美取得網站編碼

WBOY
WBOY原創
2016-07-25 09:08:53880瀏覽
基於Snoopy的PHP近似完美取得網站編碼
用於php爬蟲,取得編碼準確率99.9%, 還有部分不能獲取,求大牛完善
程式碼來源: 站雲網 www.siteyun.com
先要上網下載Snoopy.class.php
呼叫方法: echo $go->getCharset(); ?>

[代碼]url=$url; } //開啟網站 私有函數 open($url) { if($this->請求!==null) { if($this->請求->狀態==200) { 返回真; } 別的 { 返回假; } } 別的 { $this->request=new 史努比(); $this->請求->fetch($url); if($this->請求->狀態==200) { $this->請求->結果=strtolower($this->請求->結果); $charset=$this->getCharset(); if($charset!="utf-8") { if($charset=="windows-1252") { $this->請求->結果=$this->uni_decode($this->請求->結果); } 別的 { $this->請求->結果=mb_convert_encoding($this->請求->結果,"UTF-8",$charset); } } 返回真; } 別的 { 返回假; } } } //取得網站標題、關鍵字、描述 公用函數 getWebinfo() { $資訊=陣列( '標題'=>'', '關鍵字'=>'', '描述'=>'', 'ip'=>'' ); if(!$this->open($this->url)){return $info;exit;} // print_r($this->請求->結果);退出; preg_match('/([^>]*)/si', $this->請求->結果, $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->請求->結果, $match); $ft=0; foreach($match[1] 為 $mt) { if($mt=="關鍵字" || $mt=="描述") { $ft=1; } } 如果($ft==0) { preg_match_all('/"]*)"?[s]*name="?' '([^>"]*)"?[s]*[/]?[s]*>/si', $this->請求->結果, $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 = 數組(); 對於 ($i=0, $limiti=count($names); $i htmlentities($originals[$i]), '值' => $values[$i] ); } } } } 別的 { 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 = 數組(); 對於 ($i=0, $limiti=count($names); $i htmlentities($originals[$i]), '值' => $values[$i] ); } } } }$結果 = 數組 ( '元標籤' => $元標籤 ); if(isset($結果['metaTags']['關鍵字']['值'])) { $info['keywords']=$result['metaTags']['keywords']['value']; } 別的 { $info['keywords']=""; } if(isset($結果['metaTags']['描述']['值'])) { $info['desc']=$result['metaTags']['描述']['值']; } 別的 { $info['desc']=""; } $domain=preg_replace('/http:///si', '', $this->url); $ip=@gethostbyname($domain); $ip_arr=爆炸(".", $ip); 如果(計數($ip_arr)==4) { $info['ip']=$ip; } 返回$信息; } 公用函數 t($string,$o) { for($i=0;$iopen($this->url)){return false;exit;} //先從html取得編碼 preg_match("/請求->結果,$temp) ? strtolower($temp[1]):""; if($temp[1]!="") { if(in_array($temp[1], $this->charset_arr)) { if($temp[1]=="gb2312") { $tmp_charset=$this->t($this->請求->結果,$temp[1]); if($tmp_charset==$temp[1]) { 返回$temp[1]; } } 別的 { 返回$temp[1]; } } } if(!empty($this->請求->標頭)) { //從header取得編碼 $hstr=strtolower(內爆("


陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn