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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值