Я вызываю API-интерфейс linkedin и использую json.loads для преобразования JSON-объектов в списки и диктанты Python. Иногда для определенных профилей LinkedIn они пропускают указание, такое как дата окончания их университета / образования (выделено ниже). Какой механизм я бы использовал, чтобы он возвращал значение по умолчанию (т. Е. «N / A») вместо того, чтобы вызывать «Key Error» из-за отсутствия слова? Я прошу прощения за такой основной вопрос, я только начинаю с Python / Django. Я очень ценю помощь!
def home(request):
now = datetime.datetime.now()
html = "<html><body>"
token = oauth.Token(request.user.get_profile().oauth_token,request.user.get_profile().oauth_secret)
client = oauth.Client(consumer,token)
headers = {'x-li-format':'json'}
url_list = ["redacted-URL","redacted-URL", "redacted-URL"]
for x in url_list:
url = "http://api.linkedin.com/v1/people/url="+x+":(id,first-name,last-name,headline,industry,three-current-positions,**educations:(end-date,**degree,field-of-study,school-name))"
resp, content = client.request(url, "GET", headers=headers)
profile = json.loads(content)
html += profile['firstName'] + " " + profile['lastName'] + ": " + profile['headline'] + " | " + profile['industry'] + " | " + profile['threeCurrentPositions']['values'][0]['company']['name']+ " | " + profile['threeCurrentPositions']['values'][0]['title']+ " | " + profile['educations']['values'][0]['fieldOfStudy']+ " | " + profile['educations']['values'][0]['degree']+ " | " + profile['educations']['values'][0]['schoolName']+ " | " **+str(profile['educations']['values'][0]['endDate']['year'])** + "<p/>"
return HttpResponse(html)