zoukankan      html  css  js  c++  java
  • php 网络爬虫,爬一下花瓣的图片

    今天无聊看在知乎上看到有人写网络爬虫爬图片( ̄▽  ̄)

    传送门:

    福利 - 不过百行代码的爬虫爬取美女图:https://zhuanlan.zhihu.com/p/24730075

    福利 - 不过十行代码的爬虫爬取美女图:https://zhuanlan.zhihu.com/p/24779556

    其实这个是一个nodejs用了一百多行代码和php只要10行代码写的爬虫代码比较( ̄▽  ̄)

    不过这都不重要,重要的是,我看着无聊,自己又写了一个花瓣的图片爬虫( ̄▽  ̄)( ̄▽  ̄)

    注释都在代码里的,不多说了,直接上代码:

    <?php
    /**
     * 获取花瓣网图片
     * @param  string  $mixed         查询关键字或直接一个画板id
     * @param  int        $page_limit     要查询几页的,默认只查一页
     * @return void
     */
    function getHuabanImgs($mixed, $page_limit=1){
        error_reporting(0);
        set_time_limit(0);
    
        $board_id        = 0;
        $keyword         = '';
        $max             = '';
    
        if(is_numeric($mixed))
        {
            $board_id     = $mixed;
        }else
        {
            $keyword      = urlencode($mixed);
        }
    
        @mkdir('save');
    
        for ($pageno = 1 ; $pageno <= $page_limit; $pageno ++)
        {
            /*
                你喜欢用curl也行,使用这个花瓣的接口关键点是要在请求头加一个 X-Requested-With:XMLHttpRequest
                如果没有这个X-Requested-With,接口返回的是一个html网页,有的话,就是返回json
            */
            
            /*$ch     = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'http://huaban.com/search/?q='.$keyword.'&ixsaam0z&page='.$pageno.'&per_page=20&wfl=1');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:application','X-Request:JSON','X-Requested-With:XMLHttpRequest'));
            $response = curl_exec($ch);
            curl_close($ch);*/
    
            $context     = stream_context_create(array('http'=>array('method'=>'GET','header'=>"Accept:application
    X-Request:JSON
    X-Requested-With:XMLHttpRequest
    ")));
    
            //按画板id查找
            if($board_id>0)
            {
                $response     = @file_get_contents('http://huaban.com/boards/'.$board_id.'/?&max='.$max.'&limit=20&wfl=1', 'r', $context);
                $arr          = @json_decode($response, true);
                $pins         = $arr['board']['pins'];
            }
            //按关键字查找
            else
            {
                $response     = @file_get_contents('http://huaban.com/search/?q='.$keyword.'&ixsaam0z&page='.$pageno.'&per_page=20&wfl=1', 'r', $context);
                $arr          = @json_decode($response, true);
                $pins         = $arr['pins'];
            }
    
            if(empty($pins)) break;
    
            foreach ((array)$pins as $key => $value)
            {
                $type         = str_replace('image/', '', $value['file']['type']);
                if(!$type || $type=='jpeg') $type = 'jpg';
    
                $max         = $value['pin_id'];
    
                /*
                    花瓣的缩略图链接是这个的:http://img.hb.aicdn.com/c39ac6a698b6d95b823d0840a773bdb7f2cc057216dfd-HkHx3k_fw236
                    而大图的链接是这样的:http://img.hb.aicdn.com/c39ac6a698b6d95b823d0840a773bdb7f2cc057216dfd-HkHx3k
                    可以看出,其实就是去掉后面的_fw236而已,这里使用 preg_replace 正则去掉这类后缀
                */
                @copy('http://img.hb.aicdn.com/' . preg_replace('/(_[sS]+)?/', '', $value['file']['key']), './save/'.$value['file']['id'] . '.' . $type);
            }
        }
    }
    
    // getHuabanImgs('向日葵', 2);
    getHuabanImgs('13715723', 2);

    自己建个 php 文件来测试玩一下吧

  • 相关阅读:
    MySQL性能调优——索引详解与索引的优化
    Linux命令之文件搜索
    MySQL中的行级锁,表级锁,页级锁
    MySQL存储引擎
    Linux软链接和硬链接
    linux学习笔记
    在浏览器中输入一个网址后,发生了什么?
    二叉排序树
    有序表查找
    为view设置虚线边框
  • 原文地址:https://www.cnblogs.com/tujia/p/6273458.html
Copyright © 2011-2022 走看看