https://www.huobi.com/p/api/contents/
hello, i want to extract all the title from this api and see if there are any changes in the title.. how can i do it with python?
i am getting this error:
before_set = before['data']['items']['title']
TypeError: list indices must be integers or slices, not str
here is my code:
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
The data contained in before['data']['items']
is a list. You would access the first item's title using:
before['data']['items'][0]['title']
To get all titles, you can use a list comprehension:
before_set = [item['title'] for item in before['data']['items']]
you have list with ids for best visualization i recommended you compare data with id, for example:
bitems = before.get('data', {}).get('items', [])
before_data = [(item.get('id'), item.get('title')) for item in bitems]
aitems = after.get('data', {}).get('items', [])
after_data = [(item.get('id'), item.get('title')) for item in aitems]
compare = set(before_data) ^ set(after_data)
print(compare)
The issue here is that response['data']['items'] comes out as a list
type(response['data']['items'])
<class 'list'>
You can use list comprehension to get around this or try default dicts from collections
this is because before['data']['items']
has multiple items and therefore it returns an array
you can iterate over the list like:
for item in before['data']['items']:
print item['title']
or you just can save all titles into a variable like:
titles = [item['title'] for item in before['data']['items']]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.