导航:首页 > 编程语言 > php爬虫关键字

php爬虫关键字

发布时间:2022-05-20 20:56:00

php判断来访是搜索引擎蜘蛛还是普通用户的代码小结

1、推荐的一种方法:php判断搜索引擎蜘蛛爬虫还是人为访问代码,摘自Discuz x3.2
<?php
function checkrobot($useragent=''){
static $kw_spiders = array('bot', 'crawl', 'spider' ,'slurp', 'sohu-search', 'lycos', 'robozilla');
static $kw_browsers = array('msie', 'netscape', 'opera', 'konqueror', 'mozilla');

$useragent = strtolower(empty($useragent) ? $_SERVER['HTTP_USER_AGENT'] : $useragent);
if(strpos($useragent, 'http://') === false && dstrpos($useragent, $kw_browsers)) return false;
if(dstrpos($useragent, $kw_spiders)) return true;
return false;
}
function dstrpos($string, $arr, $returnvalue = false) {
if(empty($string)) return false;
foreach((array)$arr as $v) {
if(strpos($string, $v) !== false) {
$return = $returnvalue ? $v : true;
return $return;
}
}
return false;
}
if(checkrobot()){
echo '机器人爬虫';
}else{
echo '人';
}
?>

实际应用中可以这样判断,直接不是搜索引擎才执行操作
<?php
if(!checkrobot()){
//do something
}
?>

2、第二种方法:
使用PHP实现蜘蛛访问日志统计
$useragent = addslashes(strtolower($_SERVER['HTTP_USER_AGENT']));

if (strpos($useragent, 'googlebot')!== false){$bot = 'Google';}
elseif (strpos($useragent,'mediapartners-google') !== false){$bot = 'Google Adsense';}
elseif (strpos($useragent,'spider') !== false){$bot = 'Bai';}
elseif (strpos($useragent,'sogou spider') !== false){$bot = 'Sogou';}
elseif (strpos($useragent,'sogou web') !== false){$bot = 'Sogou web';}
elseif (strpos($useragent,'sosospider') !== false){$bot = 'SOSO';}
elseif (strpos($useragent,'360spider') !== false){$bot = '360Spider';}
elseif (strpos($useragent,'yahoo') !== false){$bot = 'Yahoo';}
elseif (strpos($useragent,'msn') !== false){$bot = 'MSN';}
elseif (strpos($useragent,'msnbot') !== false){$bot = 'msnbot';}
elseif (strpos($useragent,'sohu') !== false){$bot = 'Sohu';}
elseif (strpos($useragent,'yoBot') !== false){$bot = 'Yo';}
elseif (strpos($useragent,'twiceler') !== false){$bot = 'Twiceler';}
elseif (strpos($useragent,'ia_archiver') !== false){$bot = 'Alexa_';}
elseif (strpos($useragent,'iaarchiver') !== false){$bot = 'Alexa';}
elseif (strpos($useragent,'slurp') !== false){$bot = '雅虎';}
elseif (strpos($useragent,'bot') !== false){$bot = '其它蜘蛛';}
if(isset($bot)){
$fp = @fopen('bot.txt','a');
fwrite($fp,date('Y-m-d H:i:s')."\t".$_SERVER["REMOTE_ADDR"]."\t".$bot."\t".'http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]."\r\n");
fclose($fp);
}

第三种方法:
我们可以通过HTTP_USER_AGENT来判断是否是蜘蛛,搜索引擎的蜘蛛都有自己的独特标志,下面列取了一部分。
function is_crawler() {
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
$spiders = array(
'Googlebot', // Google 爬虫
'Baispider', // 网络爬虫
'Yahoo! Slurp', // 雅虎爬虫
'YoBot', // 有道爬虫
'msnbot' // Bing爬虫
// 更多爬虫关键字
);
foreach ($spiders as $spider) {
$spider = strtolower($spider);
if (strpos($userAgent, $spider) !== false) {
return true;
}
}
return false;
}

下面的php代码附带了更多的蜘蛛标识
function isCrawler() {
echo $agent= strtolower($_SERVER['HTTP_USER_AGENT']);
if (!empty($agent)) {
$spiderSite= array(
"TencentTraveler",
"Baispider+",
"BaiGame",
"Googlebot",
"msnbot",
"Sosospider+",
"Sogou web spider",
"ia_archiver",
"Yahoo! Slurp",
"YouBot",
"Yahoo Slurp",
"MSNBot",
"Java (Often spam bot)",
"BaiDuSpider",
"Voila",
"Yandex bot",
"BSpider",
"twiceler",
"Sogou Spider",
"Speedy Spider",
"Google AdSense",
"Heritrix",
"Python-urllib",
"Alexa (IA Archiver)",
"Ask",
"Exabot",
"Custo",
"OutfoxBot/YoBot",
"yacy",
"SurveyBot",
"legs",
"lwp-trivial",
"Nutch",
"StackRambler",
"The web archive (IA Archiver)",
"Perl tool",
"MJ12bot",
"Netcraft",
"MSIECrawler",
"WGet tools",
"larbin",
"Fish search",
);
foreach($spiderSite as $val) {
$str = strtolower($val);
if (strpos($agent, $str) !== false) {
return true;
}
}
} else {
return false;
}
}
if (isCrawler()){
echo "你好蜘蛛精!";
}
else{
echo "你不是蜘蛛精啊!";

⑵ PHP中怎么实现关键字搜索

PHP要实现关键字查搜索,需要用到like关键字来组合查询条件

like具体实现方法如下:

例一:

1$userForm=M('user');
1$where['name']=array('like','phpernote%');
2$userForm->where($where)->select();

这里的like查询即为:name like 'phpernote%'

例二:

1$where['name']=array('like',array('%phpernote%','%.com'),'OR');

这里的like查询即为:name like '%phpernote%' or name like '%.com'

例三:

1$where['name']=array(array('like','%a%'),array('like','%b%'),array('like','%c%'),'phpernote','or');

这里的like查询即为:(`name` LIKE '%a%') OR (`name` LIKE '%b%') OR (`name` LIKE '%c%') OR (`name` = 'phpernote')

例四:

1$where['_string']='(namelike"%phpernote%")OR(titlelike"%phpernote")'

这里的like查询即为:name like '%phpernote%' or title like '%phpernote'

⑶ 如何用PHP做网络爬虫

其实用PHP来爬会非常方便,主要是PHP的正则表达式功能在搜集页面连接方面很方便,另外PHP的fopen、file_get_contents以及libcur的函数非常方便的下载网页内容。

⑷ 想做一个网络爬虫,它能够通过关键字来爬取网页。怎么实现通过关键字来爬取网页

网络爬虫只能根据你指定的url爬取网页的html代码,至于你想要包含指定内容的网页的话,只能先爬取下来网页,然后在对页面内容进行匹配(正则,也有开源工具)找到你想要的内容就可以了!顺便说一句网络爬虫不能根据关键字来爬取网页!

⑸ php中curl爬虫 怎么样通过网页获取所有链接

本文承接上面两篇,本篇中的示例要调用到前两篇中的函数,做一个简单的URL采集。一般php采集网络数据会用file_get_contents、file和cURL。不过据说cURL会比file_get_contents、file更快更专业,更适合采集。今天就试试用cURL来获取网页上的所有链接。示例如下:

<?php
/*
* 使用curl 采集hao123.com下的所有链接。
*/
include_once('function.php');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://www.hao123.com/');
// 只需返回HTTP header
curl_setopt($ch, CURLOPT_HEADER, 1);
// 页面内容我们并不需要
// curl_setopt($ch, CURLOPT_NOBODY, 1);
// 返回结果,而不是输出它
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
$info = curl_getinfo($ch);
if ($html === false) {
echo "cURL Error: " . curl_error($ch);
}
curl_close($ch);
$linkarr = _striplinks($html);
// 主机部分,补全用
$host = 'http://www.hao123.com/';
if (is_array($linkarr)) {
foreach ($linkarr as $k => $v) {
$linkresult[$k] = _expandlinks($v, $host);
}
}
printf("<p>此页面的所有链接为:</p><pre>%s</pre>n", var_export($linkresult , true));
?>

function.php内容如下(即为上两篇中两个函数的合集):

<?php
function _striplinks($document) {
preg_match_all("'<s*as.*?hrefs*=s*(["'])?(?(1) (.*?)\1 | ([^s>]+))'isx", $document, $links);
// catenate the non-empty matches from the conditional subpattern
while (list($key, $val) = each($links[2])) {
if (!empty($val))
$match[] = $val;
} while (list($key, $val) = each($links[3])) {
if (!empty($val))
$match[] = $val;
}
// return the links
return $match;
}
/*===================================================================*
Function: _expandlinks
Purpose: expand each link into a fully qualified URL
Input: $links the links to qualify
$URI the full URI to get the base from
Output: $expandedLinks the expanded links
*===================================================================*/
function _expandlinks($links,$URI)
{
$URI_PARTS = parse_url($URI);
$host = $URI_PARTS["host"];
preg_match("/^[^?]+/",$URI,$match);
$match = preg_replace("|/[^/.]+.[^/.]+$|","",$match[0]);
$match = preg_replace("|/$|","",$match);
$match_part = parse_url($match);
$match_root =
$match_part["scheme"]."://".$match_part["host"];
$search = array( "|^http://".preg_quote($host)."|i",
"|^(/)|i",
"|^(?!http://)(?!mailto:)|i",
"|/./|",
"|/[^/]+/../|"
);
$replace = array( "",
$match_root."/",
$match."/",
"/",
"/"
);
$expandedLinks = preg_replace($search,$replace,$links);
return $expandedLinks;
}
?>

⑹ php 实现网络爬虫

  1. pcntl_fork或者swoole_process实现多进程并发。按照每个网页抓取耗时500ms,开200个进程,可以实现每秒400个页面的抓取。

  2. curl实现页面抓取,设置cookie可以实现模拟登录

  3. simple_html_dom 实现页面的解析和DOM处理

  4. 如果想要模拟浏览器,可以使用casperJS。用swoole扩展封装一个服务接口给PHP层调用

在这里有一套爬虫系统就是基于上述技术方案实现的,每天会抓取几千万个页面。

⑺ 如何用php 编写网络爬虫

php不太适合用来写网络爬虫,因为几乎没有现成的框架,或者成熟的下载机制,也不太适合做并发处理.

下载页面的话除了一个curl,就是file_get_contents,或者curl_multi来做并发请求.curl可以代理端口,虚假ip,带cookie,带header请求目标页面,下载完成之后解析页面可以用queryList来解析html.写法类似jQuery.

提供给你我之前写的类:curl.php 希望可以帮到你.

QueryList.php和phpQuery.php由于文件太大了,没办法贴上来

<?php
classHttp{
publicfunctioncurlRequest($url,$postData='',$timeOut=10,$httpHeader=array()){
$handle=curl_init();
curl_setopt($handle,CURLOPT_URL,$url);
if($httpHeader){
curl_setopt($handle,CURLOPT_HTTPHEADER,$httpHeader);
}
curl_setopt($handle,CURLOPT_RETURNTRANSFER,true);
curl_setopt($handle,CURLOPT_HEADER,0);curl_setopt($handle,CURLOPT_TIMEOUT,$timeOut);
curl_setopt($handle,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($handle,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($handle,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($handle,CURLOPT_USERAGENT,'Mozilla/5.0(Macintosh;IntelMacOSX10_7_2)AppleWebKit/537.36(KHTML,likeGecko)Chrome/27.0.1453.93Safari/537.36');curl_setopt($handle,CURLOPT_ENCODING,'gzip,deflate,sdch');
if(!empty($postData)){
curl_setopt($handle,CURLOPT_POST,1);
curl_setopt($handle,CURLOPT_POSTFIELDS,$postData);
}
$result['response']=curl_exec($handle);
$result['httpStatus']=curl_getinfo($handle,CURLINFO_HTTP_CODE);
$result['fullInfo']=curl_getinfo($handle);
$result['errorMsg']='';
$result['errorNo']=0;
if(curl_errno($handle)){
$result['errorMsg']=curl_error($handle);
$result['errorNo']=curl_errno($handle);
}
curl_close($handle);
return$result;
}
}
?>

⑻ PHP中所有的关键字有哪些

PHP关键字(68个)
and or xor __FILE__ extends __LINE__ array() as break case class const continue declare default die() do echo else elseif empty() enddeclare endfor endforeach endif endswitch endwhile eval() exit() extends for foreach function global if include include_once isset() list() new print require require_once return static switch unset() use var while __FUNCTION__ __CLASS__ __METHOD__
final(PHP 5) php_user_filter(PHP 5) interface(PHP 5) implements(PHP 5) exception(PHP 5) public(PHP 5) private(PHP 5) protected(PHP 5) abstract(PHP 5) clone(PHP 5) try(PHP 5) catch(PHP 5) throw(PHP 5) cfunction(PHP 4 only) this(PHP 5 only)

阅读全文

与php爬虫关键字相关的资料

热点内容
腾讯云应用服务器可以做什么 浏览:507
二次元表格编程 浏览:20
plc编程器保停 浏览:963
如何降低服务器的内存占用率 浏览:868
阿里云服务器是个什么意思 浏览:817
国内最好的数控编程培训学校 浏览:13
222乘104列是算法 浏览:159
程序员沟通正确姿势 浏览:969
魔玩app怎么视频推广 浏览:960
程序员抽奖送礼 浏览:458
北京java程序员薪资 浏览:658
如何创建网络平台或者app 浏览:355
python随机数生成控制概率 浏览:236
压缩机并联运行 浏览:899
两位单片机 浏览:63
四川音乐类投档线的算法 浏览:650
建行app如何改转账卡 浏览:27
android开发升级 浏览:299
要火社区app进不去怎么办 浏览:826
安卓手机上的自定义功能怎么用 浏览:230