Feb-08-2018, 06:07 AM
I am trying to extract two data items from a json web page, frustratingly without much joy.
Being fairly new to json, it is proving difficult for me as I'm probably not fully understanding the Python libraries for doing this.
When I run the following code in Python 3.5, I get the following output:
Being fairly new to json, it is proving difficult for me as I'm probably not fully understanding the Python libraries for doing this.
When I run the following code in Python 3.5, I get the following output:
Error:Python Version: 3.5.2
URL Return Code: 200
Content Type: text/plain; charset=utf-8
Traceback (most recent call last):
File "test5.py", line 30, in <module>
for key, value in data.items():
AttributeError: 'list' object has no attribute 'items'Can anyone suggest where I am going wrong.#! /usr/bin/env python3
import platform
import json
import requests
class JSONObject:
def __init__(self, d):
self.__dict__ = d
print("Python Version: %s" % platform.python_version())
# for testing, this url has the appropriate data formats
url="https://raw.githubusercontent.com/kovidgoyal/build-calibre/master/scripts/sources.json"
urlFile = requests.get(url)
print('URL Return Code: ', urlFile.status_code)
print('Content Type: ', urlFile.headers["content-type"])
#json_data = urlFile.json # NOT WORKING
#data = json.loads(urlFile.content.decode('utf-8')) #WORKS
data = requests.get(url).json() #WORKS
### Print "name" and "unix-->filename" from json data
#for key, value in json_data.iteritems():
# something???
# print("%s: %s", name, filename)
### later - store in python list and process
count = 3 # for testing just print first 3
for key, value in data.items():
print('key: {}, value: {}'.format(key, value))
count -= 1
if count == 0:
break
