파이썬 3으로 웹 API에서 정보를 얻으려고하는데 오류가 발생합니다.Json에서로드하는 방법?
import json, urllib.request, requests
def findLocation():
"""returns latlng location value in the form of a list."""
send_url = 'http://freegeoip.net/json'
r = requests.get(send_url)
j = json.loads(r.text)
lat = j['latitude']
lon = j['longitude']
return lat, lon
location = findLocation()
print(findLocation()[0])
print(findLocation()[1])
def readJsonUrl(url):
"""reads the Json returned by the google api and converts it into a format
that can be used in python."""
page = urllib.request.urlopen(url)
data_bytes = page.read()
data_str = data_bytes.decode("utf-8")
page.close()
return data_str
search =readJsonUrl("https://maps.googleapis.com/maps/api/place/textsearch/json?query=indian+restaurantsin+Coventry&location=52.4066,-1.5122&key=AIzaSyCI8n1sI4CDRnsYo3hB_oH1trfxbt2IEaw")
print(search['website'])
오류 : 어떤 도움에 감사드립니다
Traceback (most recent call last):
File "google api.py", line 28, in <module>
print(search['website'])
TypeError: string indices must be integers
그건 내 코드입니다.
업데이트 된 행을 사용하면 다른 오류가 발생합니다. 추적 (가장 최근 통화 마지막) : 파일 "google api.py", 줄 35, 인쇄 (검색 [ '결과']) KeyError : '결과' –