0

I want to pull data from here: http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getMakes

Here's my python script so far:

import urllib.request
import json
url =("http://www.carqueryapi.com/api/0.3/?callback=?&cmd=getMakes")
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
result = response.read().decode('utf-8')
print (result)

When I try to manipulate the resultes, starting with:

d = json.loads(result)

I get an error message: "ValueError: No JSON object could be decoded"

What am I doing wrong?

3
  • 2
    Can you post the output of print(result)? Commented May 14, 2016 at 19:52
  • 1
    You're going to save yourself A LOT of headaches if you use python's requests library rather than urllib to make http requests. You should provide the json data in your post. Commented May 14, 2016 at 19:53
  • just ran your code, the result is a json string wrapped in (). strip the enclosing parentheses and then you should be able to load the json. also, consider using requests instead of urllib Commented May 14, 2016 at 20:00

2 Answers 2

2

Your URL/query is wrong; you are not getting a valid JSON reply back from the server; it starts like this:

?({"Makes":[{"make_id":"abarth","make_display":"Abarth","make_is_common":"0","make_country":"Italy"},

If you modify url like this:

url = ("http://www.carqueryapi.com/api/0.3/?cmd=getMakes")

it should work. At least it worked for me after that.

Apparently, the callback=? is meant such that the client can insert a callback method which gets passed the json. I.e. you can have the server generate executable javascript rather than just json.

Sign up to request clarification or add additional context in comments.

Comments

0

If I click your URL, the first characters in the body are "?(". That's corrupting your attempt to feed it into JSON. You will need to pre-process the data to strip out the junk. There is also junk at the end.

Save yourself headaches by using Python requests library rather than urllib, by the way!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.