设为首页 收藏本站
查看: 536|回复: 0

[经验分享] php单线程爬虫类

[复制链接]

尚未签到

发表于 2018-12-11 14:22:12 | 显示全部楼层 |阅读模式

  • 代码:
    /**
    * @desc:单线程爬虫类
    * @author [Lee]
    * @property
    * 1、callcontent 获取给定url页面中的内容的回调函数
    * 2、calltodo 处理业务逻辑的回调函数 如:把抓取到的内容处理后存到数据库
    * @method
    * run 执行爬虫程序
    *     @param depth 深度 默认2
    *     @return void
    */
    class crawl{
    public $callcontent = 'getcontent';  #  获取给定url页面中的内容的回调函数
    public $calltodo = 'todo';  #  处理业务逻辑的回调函数 如:把抓取到的内容处理后存到数据库
    private $url;  #  内部属性:当前处理中的url
    /*
    @desc:内部方法,调用回调函数获取页面内容
    @param url 传入到回调函数的参数
    @return ret 页面内容
    */
    private function getcontent($url){
    $callback = $this->callcontent;
    $ret = call_user_func($callback,$url);
    return $ret;
    }
    /*
    @desc:内部方法,调用回调函数进行业务处理
    @param content 传入到回调函数的参数
    */
    private function todo($content){
    $callback = $this->calltodo;
    call_user_func($callback,$content);
    }
    /*
    @desc:内部方法,获取页面中的超链接
    @param content 页面内容
    @return urls 获取到的超链接
    */
    private function geturl($content){
    $preg = '/\'\"\ ]*).*?>/i';
    $bool = preg_match_all($preg,$content,$res);
    $urls = array();
    if($bool){
    $urls = $res[1];
    }
    $urls = array_unique($urls);
    var_dump($urls);
    }
  • 输出:
    array(72) {
    [0]=>
    string(22) "http://blog.运维网.com/"
    [2]=>
    string(30) "http://blog.运维网.com/original"
    [3]=>
    string(34) "http://blog.运维网.com/cloumn/index"
    [4]=>
    string(28) "http://blog.运维网.com/expert"
    [5]=>
    string(35) "http://blog.运维网.com/blogger/index"
    [6]=>
    string(19) "javascript:void(0);"
    [7]=>
    string(20) "http://edu.运维网.com"
    [8]=>
    string(21) "http://blog.运维网.com"
    [9]=>
    string(21) "http://down.运维网.com"
    [10]=>
    string(21) "http://home.运维网.com"
    [11]=>
    string(20) "http://bbs.运维网.com"
    [12]=>
    string(18) "http://x.运维网.com"
    [13]=>
    string(0) ""
    [14]=>
    string(20) "http://wot.运维网.com"
    [15]=>
    string(20) "http://www.运维网.com"
    [16]=>
    string(89) "http://home.运维网.com/user/register?reback=http%253A%252F%252Fblog.运维网.com%252F12173069"
    [17]=>
    string(78) "http://blog.运维网.com/user/login?reback=http%3A%2F%2Fblog.运维网.com%2F12173069"
    [18]=>
    string(12) "javascript:;"
    [19]=>
    string(34) "http://blog.运维网.com/search/index"
    [23]=>
    string(40) "http://home.运维网.com/space?uid=12163069"
    [27]=>
    string(37) "http://blog.运维网.com/12173069?type=1"
    [28]=>
    string(37) "http://blog.运维网.com/12173069?type=2"
    [29]=>
    string(37) "http://blog.运维网.com/12173069?type=3"
    [30]=>
    string(30) "http://blog.运维网.com/12173069"
    [37]=>
    string(33) "http://blog.运维网.com/12173069?s="
    [38]=>
    string(34) "http://blog.运维网.com/12173069?s=3"
    [39]=>
    string(34) "http://blog.运维网.com/12173069?s=4"
    [40]=>
    string(34) "http://blog.运维网.com/12173069?s=5"
    [41]=>
    string(34) "http://blog.运维网.com/12173069?s=6"
    [50]=>
    string(38) "http://blog.运维网.com/12173069/2126752"
    [55]=>
    string(38) "http://blog.运维网.com/12173069/2126693"
    [60]=>
    string(38) "http://blog.运维网.com/12173069/2126661"
    [65]=>
    string(38) "http://blog.运维网.com/12173069/2126657"
    [70]=>
    string(38) "http://blog.运维网.com/12173069/2126596"
    [75]=>
    string(38) "http://blog.运维网.com/12173069/2126591"
    [80]=>
    string(38) "http://blog.运维网.com/12173069/2126496"
    [85]=>
    string(38) "http://blog.运维网.com/12173069/2126420"
    [90]=>
    string(38) "http://blog.运维网.com/12173069/2126324"
    [95]=>
    string(38) "http://blog.运维网.com/12173069/2126210"
    [100]=>
    string(38) "http://blog.运维网.com/12173069/2126090"
    [105]=>
    string(38) "http://blog.运维网.com/12173069/2125724"
    [110]=>
    string(38) "http://blog.运维网.com/12173069/2125666"
    [115]=>
    string(38) "http://blog.运维网.com/12173069/2125424"
    [120]=>
    string(38) "http://blog.运维网.com/12173069/2125359"
    [125]=>
    string(38) "http://blog.运维网.com/12173069/2124937"
    [130]=>
    string(38) "http://blog.运维网.com/12173069/2124923"
    [135]=>
    string(38) "http://blog.运维网.com/12173069/2124720"
    [140]=>
    string(38) "http://blog.运维网.com/12173069/2124693"
    [145]=>
    string(38) "http://blog.运维网.com/12173069/2124499"
    [147]=>
    string(33) "http://blog.运维网.com/12173069/p1"
    [148]=>
    string(33) "http://blog.运维网.com/12173069/p2"
    [149]=>
    string(33) "http://blog.运维网.com/12173069/p3"
    [150]=>
    string(33) "http://blog.运维网.com/12173069/p4"
    [151]=>
    string(33) "http://blog.运维网.com/12173069/p5"
    [152]=>
    string(33) "http://blog.运维网.com/12173069/p6"
    [153]=>
    string(33) "http://blog.运维网.com/12173069/p7"
    [154]=>
    string(33) "http://blog.运维网.com/12173069/p8"
    [156]=>
    string(34) "http://blog.运维网.com/12173069/p19"
    [159]=>
    string(39) "http://blog.运维网.com/ityouknow/2124403"
    [160]=>
    string(35) "http://blog.运维网.com/wyait/2125708"
    [161]=>
    string(39) "http://blog.运维网.com/lumay0526/2124116"
    [162]=>
    string(38) "http://blog.运维网.com/11010461/2123639"
    [163]=>
    string(35) "http://blog.运维网.com/qiuyt/2124456"
    [164]=>
    string(30) "http://blog.运维网.com/13716231"
    [166]=>
    string(30) "http://blog.运维网.com/13108471"
    [168]=>
    string(30) "http://blog.运维网.com/10316297"
    [170]=>
    string(30) "http://blog.运维网.com/13718637"
    [172]=>
    string(30) "http://blog.运维网.com/13681316"
    [174]=>
    string(20) "http://www.运维网.com"
    [175]=>
    string(37) "http://blog.运维网.com/blogger/publish"
    [176]=>
    string(71) "http://wpa.qq.com/msgrd?v=3&uin=3591348659&site=qq&menu=yes"
    [177]=>
    string(39) "http://blog.运维网.com/运维网blog/2057444"
    }




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-650205-1-1.html 上篇帖子: php爬虫原型 下篇帖子: PHP 结合memcache make 过程的报错解决办法
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表