美文网首页
基于querylist下载新浪图片到本地

基于querylist下载新浪图片到本地

作者: LauEl | 来源:发表于2018-07-02 07:46 被阅读33次
    <?php
    /**
     * Created by PhpStorm.
     * User: 11447474@qq.com
     * Date: 2018/6/30
     * Time: 14:19
     */
    
    require 'vendor/autoload.php';
    
    use QL\QueryList;
    
    $url = 'http://www.sina.com.cn';
    $reg = [
        'img' => array('img', 'src'),
    ];
    $data = QueryList::Query($url, $reg)->data;
    ini_set('date.timezone', 'Asia/Shanghai');
    $filename = date("Ymdhis") . ".jpg";
    $urls = [];
    foreach ($data as $v) {
        foreach ($v as $value) {
            $urls[] = str_replace('//', 'http://', $value);
        }
    }
    $new_url = array();
    $str = "n.sinaimg.cn";
    foreach ($urls as $k => $v) {
        if (strpos($v, $str) != 0) {
            $new_url[] = $v;
        }
    }
    
    
    function download($url, $path = 'images/')
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        $file = curl_exec($ch);
        curl_close($ch);
        $filename = pathinfo($url, PATHINFO_BASENAME);
        $resource = fopen($path . $filename, 'a');
        fwrite($resource, $file);
        fclose($resource);
    }
    
    
    foreach ( $new_url as $url ) {
        download($url);
    }
    
    function dump($arr)
    {
        echo '<pre>';
        var_dump($arr);
        echo '</pre>';
    }
    
    ?>
    

    相关文章

      网友评论

          本文标题:基于querylist下载新浪图片到本地

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