感谢您阅读这篇文章。
我想从 JSON url 下载图像,但我不明白为什么检索功能不能正确读取我的 url。我试图将网址粘贴到检索功能中,例如:
testfile.retrieve("http://fishbase.org/images/thumbnails
/jpg/tn_Quatr_f0.jpg", "tmp/images/full/fish")
它工作得很好。
import urllib
import json
with open('fiche.json') as json_data:
d = json.load(json_data)
for obj in d:
name = json.dumps(obj['taxonomy'][0])
url = json.dumps(obj['image_urls'][0])
print(name)
print(url)
testfile = urllib.URLopener()
testfile.retrieve(url, "tmp/images/full/fish")
我现在知道它会保留我 Json 的最后一张图片,我会在解决第一个问题后修复它。
JSON :
[
{"fish_url": "http://fishbase.org/Summary/SpeciesSummary.php?id=16520", "taxonomy": ["Quassiremus ascensionis"], "image_urls": ["http://fishbase.org/images/thumbnails/gif/tn_OPHICHT0.gif"]},
{"fish_url": "http://fishbase.org/Summary/SpeciesSummary.php?id=14873", "taxonomy": ["Quinca mirifica"], "image_urls": ["http://fishbase.org/images/thumbnails/gif/tn_APOGONT0.gif"]},
{"fish_url": "http://fishbase.org/Summary/SpeciesSummary.php?id=27173", "taxonomy": ["Quassiremus polyclitellum"], "image_urls": ["http://fishbase.org/images/thumbnails/gif/tn_OPHICHT0.gif"]},
{"fish_url": "http://fishbase.org/Summary/SpeciesSummary.php?id=3896", "taxonomy": ["Quietula y-cauda"], "image_urls": ["http://fishbase.org/images/thumbnails/gif/tn_GOBIIDT0.gif"]},
{"fish_url": "http://fishbase.org/Summary/SpeciesSummary.php?id=25547", "taxonomy": ["Quassiremus evionthas"], "image_urls": ["http://fishbase.org/images/thumbnails/jpg/tn_Quevi_u0.jpg"]},
{"fish_url": "http://fishbase.org/Summary/SpeciesSummary.php?id=62532", "taxonomy": ["Quietula guaymasiae"], "image_urls": ["http://fishbase.org/images/thumbnails/jpg/tn_Qugua_u0.jpg"]},
{"fish_url": "http://fishbase.org/Summary/SpeciesSummary.php?id=13924", "taxonomy": ["Quassiremus nothochir"], "image_urls": ["http://fishbase.org/images/thumbnails/jpg/tn_Qunot_u1.jpg"]},
{"fish_url": "http://fishbase.org/Summary/SpeciesSummary.php?id=62338", "taxonomy": ["Qianlabeo striatus"], "image_urls": ["http://fishbase.org/images/thumbnails/gif/tn_CYPRINT0.gif"]},
{"fish_url": "http://fishbase.org/Summary/SpeciesSummary.php?id=27728", "taxonomy": ["Quintana atrizona"], "image_urls": ["http://fishbase.org/images/thumbnails/jpg/tn_Quatr_f0.jpg"]}
]
我的请求的结果是:
"Quassiremus ascensionis"
"http://fishbase.org/images/thumbnails/gif/tn_OPHICHT0.gif"
Traceback (most recent call last):
File "dlimg.py", line 12, in <module>
testfile.retrieve(url, "tmp/images/full/fish")
File "/usr/lib/python2.7/urllib.py", line 245, in retrieve
fp = self.open(url, data)
File "/usr/lib/python2.7/urllib.py", line 210, in open
return self.open_unknown(fullurl, data)
File "/usr/lib/python2.7/urllib.py", line 222, in open_unknown
raise IOError, ('url error', 'unknown url type', type)
IOError: [Errno url error] unknown url type: '%22http'
我搜索了一个解决方案,但我花了大约 1 个小时却没有找到任何东西。
谢谢你的回答:)
您的网址中似乎有“引号
IOError: [Errno url error] unknown url type: '%22http'
你看到 %22http
试试这个
testfile.retrieve(url.replace('"',''), "tmp/images/full/fish")
我希望它修复
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句