簡體   English   中英

Json接受了1個位置參數,但給出了2個

[英]Json takes 1 positional argument but 2 were given

我收到一個錯誤,即時通訊給了1個參數,但給出了2個參數,最終目標是打印json正文

url_get = 'http://ludwig.corp.podiumdata.com:/qdc/entity/v1/getEntities?type=EXTERNAL&count=2&sortAttr=name&sortDir=ASC'
session = requests.Session()
r = session.get(url_cookie, auth=(username,password), verify=False)
print('--------------------- 1. status_code ----------------------------------')
print(r.status_code)
print('--------------------- 1. headers ----------------------------------')
print(r.headers)
print('--------------------- 1. content ----------------------------------')
data = r.json(url_get)
print(data)
print('--------------------- 1. cookies ----------------------------------')
print(session.cookies, r.cookies)
print('--------------------- 1. cookies get_dict ----------------------------------')
print(session.cookies.get_dict())
print("7")
  File "<ipython-input-36-b971c3b17ea5>", line 67, in <module>
    data = r.json(url_get)

TypeError: json() takes 1 positional argument but 2 were given```

這是因為您嘗試在URL上調用json.loads ,該URL不適合python中的任何有效對象。

import json

x = json.loads('[1, 2, 3]')
x  # This gets evaluated to a list
[1, 2, 3]

y = json.loads('hi i am a string')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mm92400/anaconda3/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/Users/mm92400/anaconda3/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/Users/mm92400/anaconda3/lib/python3.6/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)

我確定該錯誤消息看起來很熟悉:)

接下來,當您在Request對象上調用json方法時,您無需傳遞任何內容,因為請求的主體將隱式傳遞給json解析器:

r = requests.get(some_url)
r.json() # don't put anything in the parens here

json的文檔在這里requests這里

您的意思是說r.json() r是代表從服務器返回的數據的響應對象。 調用json()方法(嘗試)將JSON中的數據解析為python數據結構。 您不能將任何其他參數傳遞給該函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM