Blankspider PHP 爬虫
程序员文章站
2022-06-04 09:00:22
...
php代码
function spider($url, $spider = 'Blankspider', $port = 80, $timeout = 15) { $content= ''; $resolve = parse_url($url); $host = $resolve['host']; $path = empty($resolve['path']) ? '/' : $resolve['path'].(!empty($resolve['query']) ? '?'.$resolve['query'] : ''); if(empty($host)) { return 'Requested host name can\'t be empty'; } $fp = fsockopen($host, $port, $errno, $errstr, $timeout); if (!$fp) { return $errstr; } else { $fputs = "GET $path HTTP/1.1\r\n"; $fputs.= "Accept: */*\r\n"; $fputs.= "Host: $host\r\n"; $fputs.= "Spider: $spider\r\n"; $fputs.= "Connection: Close\r\n\r\n"; stream_set_blocking($fp, 1); stream_set_timeout($fp, $timeout); fputs($fp, $fputs); while(!feof($fp)) { if(($return = fgets($fp)) && ($return == "\r\n" || $return == "\n")) { break; } } while(!feof($fp)) { $content .= fgets($fp, 8192); } fclose($fp); return $content; } } function cron2spider($iptable, $sleep = 5) { set_time_limit(0); $i = 0; date_default_timezone_set('PRC'); if(!file_exists($iptable)) { return json_encode(array( 'status'=> 'error', 'description'=> 'iptable.conf file not exists')); } $file = file($iptable); if(empty($file)) { return json_encode(array( 'status'=> 'error', 'description'=> 'iptable.conf can\'t be empty')); } while($i< count($file)) { if(!file_exists('robots') || !is_writable('robots')) { return json_encode(array( 'status'=> 'error', 'description'=> 'directory doesn\'t exist or don\'t have write permissions')); } $dir = 'robots/'.preg_replace('/(http\:\/\/)|(\s)|(www\.)/', '', $file[$i]); if(!file_exists($dir)){ mkdir($dir); } file_put_contents($dir.'/'.date('Y.m.d.H.i.s', time()).'.txt', spider(preg_replace('/\s/', '', $file[$i]))); $i++; sleep($sleep); } return json_encode(array( 'status'=> 'ok', 'description'=> 'robots program execution success')); } echo cron2spider('iptable.conf');
上一篇: MySQL复习笔记I_MySQL
下一篇: python文件操作之目录遍历实例分析
推荐阅读
-
Linux下PHP连接Oracle数据库_PHP
-
PHP使用empty检查函数返回结果时报Fatal error: Can't use function return value in write context的问题 - 心中的飞梦
-
PHP安装memcached扩展笔记_PHP
-
PHP错误Warning:mysql_query()解决方法_PHP
-
php实现utf-8转unicode函数代码实例
-
【性能优化】PHP代码输出压缩后HTML
-
记录页面执行时间php代码
-
PHP实现导出excel数据的类库用法
-
CodeIgniter控制器之业务逻辑实例分析,codeigniter控制器_PHP教程
-
关于PHP Notice: A non well formed numeric value encountered,