首页 > 代码库 > python 图片抓取

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()

python 图片抓取