这个方法会返回一个url吗?

时间:2016-10-04 14:29:11

标签: python json url request google-places-api

抱歉,如果这是一个愚蠢的问题,我正在学习,请原谅我。 我得到了这个方法(更新):

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

我使用Google Places API - Place Photo构建了requests.get。 在我的__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获取照片,但我尝试了,它返回无。 我想知道get_photo方法返回什么,如果它不是网址。如何将其编码为网址?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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)