0

I'm very new to Python, I'm trying to read json data from an API call. The problem is an error saying TypeError: <http.client.HTTPResponse object at 0x030CF310> is not JSON serializable however the data I'm trying to pull from is the correct format, https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json . Here's the code I'm trying to run.

import urllib.request
import json

url = "https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&prop=revisions&rvprop=content&format=json"
req = urllib.request.urlopen(url)
page = json.dumps(req)
print(page)
CS2016
  • 331
  • 1
  • 3
  • 15
  • `urlopen()` returns a response object (what you're calling `req`). The content of the response is in `req.read()`, not in `req` itself. – John Gordon Nov 06 '16 at 01:07
  • The dupe will give you exactly what you want. If you want to explore other modules in the Python world, check out [requests](http://docs.python-requests.org/en/master/). With that, you can just do: `requests.get(url).json()` – idjaw Nov 06 '16 at 01:08
  • That answered it, thank you! – CS2016 Nov 06 '16 at 01:09

0 Answers0