码迷,mamicode.com
首页 > 编程语言 > 详细

python 图片抓取

时间:2017-06-21 11:25:11      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:dir   exce   地址   default   down   md5   images   mkdir   nts   

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 图片抓取

标签:dir   exce   地址   default   down   md5   images   mkdir   nts   

原文地址:http://www.cnblogs.com/sy-liu/p/7058174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!