zoukankan      html  css  js  c++  java
  • python爬虫入门---第三篇:保存一张图片

    import requests
    import os
    
    
    url = 'http://imgsrc.baidu.com/forum/w%3D580%3B/sign=749ed018cecec3fd8b3ea77de6b3d63f/83025aafa40f4bfb3661b3800e4f78f0f63618b4.jpg'
    root_path = r'D:picturezhaoliying'
    
    #利用split()函数获取url最后的文件名
    img_name = url.split('/')[-1]
    
    img_path = root_path + r'{0}'.format(img_name)
    
    try:
      #如果根目录不存在就创建该根目录
    if not os.path.exists(root_path): os.makedirs(root_path) if not os.path.exists(img_path): r = requests.get(url) with open(img_path, 'wb') as f: f.write(r.content) f.close() print("文件保存成功") else: print("文件已存在") except: print("执行出错")

    -----------------------------------------------------------------
    输出结果:
    文件保存成功
    [Finished in 0.5s]

    蒹葭苍苍,白露为霜; 所谓伊人,在水一方。
  • 相关阅读:
    Reducing File Size
    程序设计中的命名
    代码进阶
    如何显示当前Mipmap级别?
    何时使用泛型集合
    Using Mono DLLs in a Unity Project
    lock关键字
    VSS/RSS/PSS/USS
    AssetBundle依赖
    WWW.LoadFromCacheOrDownload
  • 原文地址:https://www.cnblogs.com/huwt/p/10373142.html
Copyright © 2011-2022 走看看