YasserKhalil
YasserKhalil

Reputation: 9568

How to return JSON format from urlopen() response?

I am newbie to python and when trying such a code, I got weird characters.

import json
from urllib.request import urlopen

with urlopen(
        "http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=MSFT&region=1&lang=en") \
            as response:
    source = response.read()
    print(source)

I expect the response to be JSON format but I got weird response like this:

enter image description here

Upvotes: 0

Views: 144

Answers (1)

That1Guy
That1Guy

Reputation: 7233

I got the expected output using the following with requests:

import requests

url = 'http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=MSFT&region=1&lang=en'

rsp = requests.get(url)
print(rsp.json())

Upvotes: 2

Related Questions