【发布时间】:2018-04-26 06:21:15
【问题描述】:
https://www.huobi.com/p/api/contents/
你好,我想从这个api中提取所有标题,看看标题是否有任何变化..我如何用python做到这一点?
我收到此错误:
before_set = before['data']['items']['title']
TypeError: list indices must be integers or slices, not str
这是我的代码:
import requests
import json
try:
with open('notice.json', 'r') as current_notice:
before = json.loads(current_notice.read())
except IOError:
before = requests.get('https://www.huobi.com/p/api/contents/').json()
with open('notice.json', 'w') as current_notice:
current_notice.write(json.dumps(before))
print("First run....")
after = requests.get('https://www.huobi.com/p/api/contents/').json()
before_set = before['data']['items']['title']
after_set = after['data']['items']['title']
new_set = after_set - before_set
while True:
try:
if not new_set:
print("No change... Exiting.")
if new_set:
print("There are changes")
except Exception as e:
print(e)
pass
【问题讨论】: