2016-10-04 81 views
0

对不起,如果这是一个愚蠢的问题,我正在学习如此原谅我。 我得到这个方法(更新):此方法是否返回一个url?

def get_photo(self, photo_reference): 
    print(self.photourl) 
    print(photo_reference) 
    resp = requests.get(
     url='{}{}'.format(self.photourl, photo_reference), 
     params={'key': self.api_key} 
    ) 
    try: 
     return resp.json() 
    except ValueError: 
     print('Invalid JSON') 

我建立了requests.get与Google Places API - Place Photo。 在我__init__(self):

self.place_detail_url = 'https://maps.googleapis.com/maps/api/place/' 
self.ref = 'photo?maxwidth=200&photoreference=' 
self.photourl = self.place_detail_url + self.ref 

我想从谷歌的地方API的照片,但我想,它返回None。 我不知道get_photo方法返回什么,如果它不是一个网址。我怎样才能将它编码到一个网址?

+0

你应该从'get_photo()'返回一些东西吗?.. – alecxe

+0

该方法里没有'return'语句,所以它会返回默认的'None'。我怀疑你应该分配'requests.get'调用返回的值并返回它,它应该是一个包含你想要的照片数据的对象(它也包含其他有用的东西)。 –

+0

你想下载图片吗? requests.get(url).content您需要将其与一个打开的命令组合在一起以保存文件:'open(file,'wb')as file:file.write(requests.get(url).content) ' – Lost

回答

1

我有我的回答,resp是一个对象,并根据HTTP状态200,我的网址是好的。所以我只需要做return resp.url以获得正确的URL链接。这是一个小问题,但我做到了这一点。对不起,谢谢你的答案!

0

也许尝试这(对你看似首选方法基于上述建立的网址你自己的):

def get_photo(self, photo_reference): 
    request_url = (self.place_detail_url + self.ref + photo_reference + '&key=' + self.api_key) 
    output_file = (/path/to/image/here/file.png) 
    with open(output_file, 'wb') as output: 
     output.write(requests.get(request_url).content) 
+0

其实我想让它返回一个字符串(https链接)。因为我想在参数中放入'image_url',并将其发送回Facebook Messenger API [link](https://developers.facebook.com/docs/messenger-platform/send-api-reference/generic-template) 。你有什么主意吗?我知道如何从json中获得价值,但是这个Google Places Photo返回了不同的东西。我知道它不会返回一个json响应。 –

+0

我想'打印(STR(RESP))'和我得到这个回'的https://maps.googleapis.com/maps/api/place/photo maxwidth = 200&photoreference ='&'CoQBcwAAAKoYnKhY40-G1p55pNqMnQrKGKpzD9_9abee8YUMO72VoWfl5CCAyI5iL3mU1BoUIXtlM1vOCar7ZpIRZAOjui5ByS70Ha7VN1z9Y8446xUxu_8PSgb5VaoxtlzXxTO63fC6dFZzEeX8R5Aos1ucVn0-yUbZxuLYgx6uLOopj1jWEhAZ9e6FBFOvWcXTQ51qp_LxGhRbJT1D6CCmaTIlpZvABh5QhXn8dQ'?长字符串是photo_reference,我在浏览器中检查过它并返回'https://lh3.googleusercontent。COM/-es-54JIkYqQ/VvcJjK4fJbI/AAAAAAASeZE/T4whpXc9rDsjsk1tjOG3nmLqYATo2zcVw/S1600-W200 /' –

相关问题