使用urllib3获取JSON数据

时间:2018-01-18 16:29:41

标签: python json urllib3

我已经在PHP中编写了自己的URL缩短程序,现在我正在尝试为它开发一个python前端。我的python脚本如下:

import certifi
import urllib3
import json
manager = urllib3.PoolManager(
    cert_reqs='CERT_REQUIRED',
    ca_certs=certifi.where())
url = input("Please enter a URL: ")
short = manager.request("GET", "https://www.get-short.net/feed.php?i=" + url)
data = short.read(decode_content=True)
print(json.loads(data))

并且,当我运行它时,当我输入URL

时,我得到以下输出
c:\Projects>python short.py
Please enter a URL: https://docs.python.org/2/library/json.html
Traceback (most recent call last):
File "short.py", line 10, in <module>
    print(json.loads(data))
File "C:\Program Files\Python36\lib\json\__init__.py", line 354, in loads
    return _default_decoder.decode(s)
File "C:\Program Files\Python36\lib\json\decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files\Python36\lib\json\decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我知道问题是我的代码或我的JSON输出,但我需要做些什么来使其工作

1 个答案:

答案 0 :(得分:0)

我相信你想使用short.data代替short.read

short.read返回什么类型?