zoukankan      html  css  js  c++  java
  • 简单实现图片抓取下载到本地①

      通过url将图片下载到本地或者服务器中

    1、抓取图片的信息到本地,和之前讲的数据抓取方式一样(可以采用其他的两种方式,随心所欲,不过后面会讲到多线程下载图片需要用到curl)

    $ch = curl_init();
    $url = 'http://图片链接';
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  
    $file_contents = curl_exec($ch); 
    curl_close($ch);

    2、将数据放入文件中,并命名为图片格式

    $file = fopen('a.png','w');
    fwrite($file,$file_content);
    fclose($file);

      如果连上面的三个函数都嫌麻烦,可以直接使用file_put_contents

    file_put_contents('a.png',$file_content);

    3、查看当前目录中的图片。over

  • 相关阅读:
    在ubuntu系统使用SSR
    Pandas库
    Numpy
    06-Python之标准库
    do{}while(0)
    inet_XX族函数
    大端小端
    c++ 强制类型转换
    auto类型推导
    const浅析
  • 原文地址:https://www.cnblogs.com/yaradish/p/9513824.html
Copyright © 2011-2022 走看看