小编Dav*_*ira的帖子

使用python(urllib/urllib2)下载图片很慢

我正在尝试从 scryfall.com 下载魔法收集卡的图像。他们为这个 json 文件提供了关于每张卡片的所有信息(包括其图像的 url)。所以我写了一个代码,从那个 json 文件中读取每个 url,并尝试保存它。问题是,代码的请求部分运行每个图像需要超过 5 分钟,我不知道为什么。(我正在获取的每个图像的大小小于 100kB 并且在浏览器上立即打开)

我试过 urllib.urlretrieve、urllib2.urlopen,都是一样的。尝试在 python2 和 python3 上运行它。

没有错误消息,代码确实有效,只是它花费的时间太长,无法继续执行。

编辑:

a=open("cards.json")
b=a.read()

data=[]
data.append(b)

count=0
for elem in data:
    try:
        content=json.loads(elem)
    except:
        print content
        exit()
    for j in content:
        count=count+1
        if j['layout']=='normal' and j['digital']==False:
            url=str(j['image_uris']['normal'])
            final=url[url.find('normal')+6:]
            print (url)
            print("a")
            i1=urllib.urlretrieve(url)
            print("b")
            i2=i1.read()
            file=open(str(count),'wb')
            file.write(i2)
            file.close()


        if count>5:
            exit()
Run Code Online (Sandbox Code Playgroud)

编辑 2:我正在使用的 json 的链接:https : //archive.scryfall.com/json/scryfall-default-cards.json

python urllib urllib2

1
推荐指数
1
解决办法
1317
查看次数

标签 统计

python ×1

urllib ×1

urllib2 ×1