您好社區成員,
我對 python 語言和編程非常陌生,目前我正在開發一個顯示來自該 API 的新聞的新聞 API。我希望這個程式在 API 有任何更新時檢查和更新。請幫助我能做些什么來完成這個。
代碼:
url = 'https://cryptopanic.com/api/v1/posts/?auth_token=<my token>&filter=hot'
html_link = requests.get(url)
datatype = html_link.json()
news_info = datatype['results']
latest_news = news_info[0]['title']
source = news_info[0]['source']['title']
print(latest_news)
我想要這個 latest_news 變數,它存盤在串列中有新新聞時要列印的新聞,我嘗試了比較方法,但到目前為止仍然沒有找到任何東西。
uj5u.com熱心網友回復:
這是否符合您的標準?您必須每 5 分鐘或任何時間運行一次,您將獲得最新的標題。
import requests, json
old_news_info = {"news": []}
try:
old_news_info = json.load(open("old_news_info.json", "r"))
except:
pass
url = 'https://cryptopanic.com/api/v1/posts/?auth_token=<token>&filter=hot'
print("waiting for response")
html_link = requests.get(url)
datatype = html_link.json()
if datatype != {'status': 'Incomplete', 'info': 'Token not found'}:
news_info = datatype['results']
if not news_info[0] in old_news_info["news"]:
for news in news_info:
if news in old_news_info["news"]:
break
else:
old_news_info["news"].append(news)
print(news["source"]['title'])
json.dump(old_news_info, open("old_news_info.json", "w"), indent = 4)
else:
print("Token not found")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/422831.html
標籤:
