简体   繁体   中英

python api Can't load Str with Json.load()

I've just started learning about json in python and start practicing getting bitcoin price from the website; however, I keep getting this error:

''Exception Value: Expecting value: line 1 column 1 (char 0)''

When I print source it shows b' in the front of the string. After browsing online I think it's a problem regarding byte and str stuff(also checked from an online website that the json format is indeed correct). My code is as follows:

import json
import urllib.request

url = 'http://www.quandl.com/api/v3/datasets/BCHARTS/BITSTAMPUSD'

with urllib.request.urlopen(url) as response:
    source = response.read().decode('utf-8')
data = json.loads(source)

Currently, I switch to

data = requests.get(url).json()

and works smoothly, still I wish to fix the above error. Thanks in advance!

You are getting this error because the response is in HTML format. Not in JSON format.

You can print the source variable and have a look.

The actual JSON is inside the <code> tag:

<html class="gr__quandl_com"><head>
<head>...</head>
<body>
    <pre>
        <code>
        {...}             #Here is the actual JSON.
        </code>
    </pre>
</body>

You can use libs such as beautifulsoup to parse HTML and extract the JSON before creating the JSON object.


Alternatively, you can ask the server to send the response in JSON format by adding the following header while making the request:

request.add_header('Accept', 'application/json') 

You're getting an error, because the response from the server is normal HTML and the data you're looking for is inside a <pre> tag. You'll have to extract the content of the <pre> tag or include the appropriate headers when making the request.

From quandl API docs
Change Formats You can get the same data in JSON format:

https://www.quandl.com/api/v3/datasets/OPEC/ORB.json
Or in XML: https://www.quandl.com/api/v3/datasets/OPEC/ORB.xml

So in your case it would be:

url = 'http://www.quandl.com/api/v3/datasets/BCHARTS/BITSTAMPUSD.json'

You need to tell the server, you want response in JSON format. Currently you are getting HTML.

import json 
import urllib.request 

url = 'http://www.quandl.com/api/v3/datasets/BCHARTS/BITSTAMPUSD' 
request = urllib.request.Request(url) 
request.add_header('Accept', 'application/json') 
with urllib.request.urlopen(request) as response: 
     source = response.read().decode('utf-8') 
data = json.loads(source)           

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM