• python 图片抓取


    1、图片地址为下载地址访问图片地址可直接下载的

      #!/usr/bin/env python
      # -*- coding: utf-8 -*-
      import urllib2
      import json
      import requests
      import os
      import sys
      from time import sleep
      reload(sys)
      sys.setdefaultencoding("utf-8")
      def uploadreColorImg():
      file_recolorJson = "/home/siyin/Downloads/LibraryMD5.json"
      recolor_url = "basicurl"
      savePath = "/home/siyin/recolor0621/"
      with open(file_recolorJson) as f:
      j = json.loads(f.read())
      books = j.get("Books")
      for item in books:
      count = 0
      category = item
      os.mkdir(savePath+category,0775)
      os.mkdir(savePath + category+"/c", 0775)
      imglist = books.get(item)
      for elem in imglist:
      contents = imglist.get(elem).get("Contents")
      for ele in contents :
      count = count + 1
      art = contents.get("Art")
      artUrl = recolor_url + art
      try:
      print artUrl
      r_art = requests.get(artUrl)
      print r_art.status_code
      if r_art.status_code == 200:
      with open(savePath+category+"/"+art+".pdf","wb") as output:
      for chunk in r_art:
      output.write(chunk)
      except:
      sleep(5)
      continue
      finally:
      output.close()
      print count

      if __name__ == "__main__":

      uploadreColorImg()

    2、url是图片链接

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    import json
    import requests
    import os
    import sys
    import time
    import urllib
    reload(sys)
    sys.setdefaultencoding("utf-8")

    def getAndSaveImg(urlParam,save_absPath):

    urllib.urlretrieve(urlParam,save_absPath)

    def uploadColorImg():

    file_json = "/home/siyin/colorfy/colorfy_json.json"
    color_url = "http://cdn.colorfy.fungames-forfree.com/v2/android/galleries/images/"
    savePath = "/home/siyin/colorfy/"
    with open(file_json) as f:
    j = json.loads(f.read())
    galleries = j.get("galleries")
    for gallery in galleries:
    galleryId = gallery.get("id")
    os.mkdir(savePath+galleryId,0775)
    volumes = gallery.get("volumes")
    if volumes is not None:
    for volume in volumes:
    volumeId = volume.get("id")
    paintings = volume.get("paintings")
    for painting in paintings:
    img_path = painting.get("img_path")
    img_url = color_url + img_path + '.png'
    save_absPath = savePath + galleryId + "/" + img_path + '.png'
    try:
    r_img = requests.get(img_url)
    if r_img.status_code == 200:
    getAndSaveImg(img_url,save_absPath)
    else:
    print "error"
    except:
    time.sleep(5)
    continue
    print img_path

    if __name__ == "__main__":
    uploadColorImg()

  • 相关阅读:
    CORS跨域解决方案
    修改数据库排序规则实践总结
    【转】通俗易懂,什么是.NET?什么是.NET Framework?什么是.NET Core?
    调用远程数据库的T-SQL和SP(SQLSERVER)
    解决在微信网页中百度地图定位不准确的问题
    VUE小知识点
    实现鼠标移过时,显示图片的功能
    实现导出功能
    两数据库表之间迁移插入数据
    IIS配置FTP
  • 原文地址:https://www.cnblogs.com/sy-liu/p/7058174.html
Copyright © 2020-2023  润新知