如何在python3中打印post请求中的json数据?

5cg8jx4n  于 2023-01-18  发布在  Python
关注(0)|答案(1)|浏览(203)

下面是我的代码:

from requests import post

peer2profit = post("https://peer2profit.com/api/getpeers",data={"_token":"mytoken"}) 
print(peer2profit.json())

下面是我期望的输出:

{
    "totalPeer": 10,
    "balance": 0.48045,
    "peers": [
        {
            "id": 108270438,
            "serial": null,
            "online": 1,
            "territorial_dispute": "",
            "version": "3.4bA",
            "platform": "ANDROID",
            "ip": "my ip",
            "country": "HR",

  And much more...

这是我得到的:

Traceback (most recent call last):
  File "c:\Users\skepp\Desktop\test.py", line 24, in <module>
    peer2profit = post("https://peer2profit.com/api/getpeers",headers={'accept': 'application/json'},data={"_token": "my token"}).json()['peers']
  File "C:\Users\skepp\AppData\Local\Programs\Python\Python310\lib\site-packages\requests\models.py", line 910, in json
    return complexjson.loads(self.text, **kwargs)
  File "C:\Users\skepp\AppData\Local\Programs\Python\Python310\lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "C:\Users\skepp\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\skepp\AppData\Local\Programs\Python\Python310\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

我有这个错误之前,但我不知道如何修复它!
我在2个不同的HTTP请求工具中尝试了这个帖子请求,它成功了!
以下是一些图片:RestManRequestttp

yzuktlbb

yzuktlbb1#

我没有看到您导入JSON并解析它。

from requests import post
import json

peer2profit = post("https://peer2profit.com/api/getpeers",data={"_token":"mytoken"}) 

# Parse it
parsed = json.loads(peer2profit)

# Output the entire dictionary 
print(parsed.json())

相关问题