美文网首页
php 正则抓取百度图片

php 正则抓取百度图片

作者: luowenbin | 来源:发表于2018-10-21 23:36 被阅读53次
    <?php
    
    //获取网页
    function get_ssl_html($url) {
        //file_get_contents()
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    function get_thing_images_url_baidu($th) {//获取食材图片url
        $thing = urlencode($th);
        $url = "https://image.baidu.com/search/index?tn=baiduimage&word=$thing";
        $html = get_ssl_html($url);
        $pattern = '/thumbURL":"(.+?)"/is';
        preg_match_all($pattern, $html, $match);
    
        $arr=array();
        foreach ($match[1] as $img){
            if (strstr($img,"jpg")) {
                $arr[]=$img;
            }
        }
    
        return $arr;
    }
    
    
    var_dump( get_thing_images_url_baidu("瑞士卷"));
    
    

    相关文章

      网友评论

          本文标题:php 正则抓取百度图片

          本文链接:https://www.haomeiwen.com/subject/uftwzftx.html