简体   繁体   English

如何从 urlopen() 响应返回 JSON 格式?

[英]How to return JSON format from urlopen() response?

I am newbie to python and when trying such a code, I got weird characters.我是 python 的新手,在尝试这样的代码时,我得到了奇怪的字符。

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:我希望响应是 JSON 格式,但我得到了这样的奇怪响应:

在此处输入图片说明

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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