1. 程式人生 > >Python之JSON數據解析實例:新聞頭條

Python之JSON數據解析實例:新聞頭條

mage value 相關 轉化 -s 合數 示例 json數據 type

一、接口相關

數據服務商:聚合數據(https://www.juhe.cn/

API部分文檔:

技術分享圖片

完整API文檔下載:https://files.cnblogs.com/files/qikeyishu/%E6%96%B0%E9%97%BB%E5%A4%B4%E6%9D%A1.pdf

二、代碼實現

view.py:(部分代碼-後臺)

@home.route("/")
def index():
# 接口地址(PS:需要用自己的key value)
url = ‘http://v.juhe.cn/toutiao/index?type=top&key=‘
# 發送get請求
r = requests.get(url)

# 轉化為json對象
data = r.json()
# json對象傳入前臺
return render_template(‘home/index.html‘, data=data)

index.html:(部分代碼-前臺頁面渲染)

<div class="panel-body">
<ul class="list-group">
{% for v in data[‘result‘][‘data‘]%}
<li class="list-group-item">
<img width="100px" src="{{v.thumbnail_pic_s
}}" alt="..." class="img-thumbnail">

<a href="{{v.url}}">{{v.title}}</a><br/>
<span>作者:{{v.author_name}}&nbsp;&nbsp;&nbsp;{{v.date}}</span>
</li>
{% endfor%}
</ul>
</div>

效果圖示例:

技術分享圖片

PS:文章轉載請註明出處,感謝您的合作!

Python之JSON數據解析實例:新聞頭條