[英]Displaying stackOverflow API JSON data using Flask
我一直在嘗試從StackOverflow API檢索到的JSON數據中顯示我的用戶名和信譽。
我使用python模塊請求來檢索數據。 這是代碼
from flask import Flask,jsonify
import requests
import simplejson
import json
app = Flask(__name__)
@app.route("/")
def home():
uri = "https://api.stackexchange.com/2.0/users? order=desc&sort=reputation&inname=fuchida&site=stackoverflow"
try:
uResponse = requests.get(uri)
except requests.ConnectionError:
return "Connection Error"
Jresponse = uResponse.text
return Jresponse
if __name__ == "__main__":
app.run(debug = True)
未使用的導入是我完成這項工作所需要的,但似乎無法知道如何完成它。 下面是返回瀏覽器的內容,我想只顯示用戶名[display_name]和聲譽。 我有什么選擇來完成這項工作?
{ “物品”:[{ “USER_ID”:540028 “USER_TYPE”: “注冊”, “CREATION_DATE”:1292207782, “DISPLAY_NAME”: “Fuchida”, “profile_image”:“http://www.gravatar.com/化身/ 6842025a595825e2de75dfc3058f0bee d = identicon&R = PG”, “信譽?”:13, “reputation_change_day”:0 “reputation_change_week”:0 “reputation_change_month”:0 “reputation_change_quarter”:0 “reputation_change_year”:0, “年齡” :24, “last_access_date”:1332905685, “LAST_MODIFIED_DATE”:1332302766, “is_employee”:假的, “鏈接”: “http://stackoverflow.com/users/540028/fuchida”, “WEBSITE_URL”:“HTTP:// blog.Fuchida.me“,”location“:”Minneapolis MN“,”account_id“:258084,”badge_counts“:{”gold“:0,”silver“:0,”bronze“:3}}],”quota_remaining “:282,” quota_max “:300,” has_more“:假}
使用json.loads()讀取和解碼數據。
from flask import Flask,jsonify
import requests
import simplejson
import json
app = Flask(__name__)
@app.route("/")
def home():
uri = "https://api.stackexchange.com/2.0/users? order=desc&sort=reputation&inname=fuchida&site=stackoverflow"
try:
uResponse = requests.get(uri)
except requests.ConnectionError:
return "Connection Error"
Jresponse = uResponse.text
data = json.loads(Jresponse)
displayName = data['items'][0]['display_name']# <-- The display name
reputation = data['items'][0]['reputation']# <-- The reputation
return Jresponse
if __name__ == "__main__":
app.run(debug = True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.