我正在尋找一種方式來從bing地圖提供一些圖像而不共享URL(因爲它包含密鑰)並將其返回給客戶端(不先保存到磁盤)。我用這個方法從here響應流[200 OK]>不是JSON可串行化流圖像python
def get_stream_from_url(url):
req = requests.get(url, stream = True)
return Response(stream_with_context(req.iter_content()), content_type = req.headers['content-type'])
我使用的另一種方法,我在那裏生成對於那些像那些信息的列表,所以基本上我婉把JSON內sream響應,值關鍵url
:
def images_data_and_url(self, data):
results = []
for i in range(1, 13):
image = {}
description = data['description']
url = self.get_stream_from_url(data['url'])
tile = {"description": description
"url": url}
results.append(image)
print results
return json.dumps(results)
編輯: 如果我打印結果我有這樣的:
<type 'list'>: [{'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>} , {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>},{'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}, {'description': ' this is an example', 'url': <Response streamed [200 OK]>}]
如果我不喜歡這個,我得到這個呃ROR
<Response streamed [200 OK]> is not JSON serializable
因爲get_stream_from_url
返回響應函數的這個:/我的問題是,我不知道如何以另一種方式把它?
在HTML中我得到的數據,每個數據分配爲這樣的每個圖像:
<img class="image1 " data-description="The description" src="">
有人可以幫我,如何將流編碼,所以我可以把它作爲與JSON數據?
當你在'images_data_and_url'函數的'return'之前執行'print(results)'時,看起來它是什麼樣子? – linusg
謝謝,我明白了。正如你可能已經注意到的那樣,這些網址很棒,所以我會從那裏開始。也許你做得太複雜了,這就是爲什麼它打破了... – linusg
@linusg網址是好的,因爲我測試了它? – Ritchie