request报错
发表在Python答疑区
2022-01-13 悬赏:3 学分
是否精华
是
否
版块置顶:
是
否
和那个快手爬票类似的requests请求代码,报错:
[ WARN:0@11.818] global D:\a\opencv-python\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (539) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback
Trace back(most recent call last):
self.prepare_url(url, params)
File "C:\Users\Administrator\PycharmProjects\Project\venv\python\lib\site-packages\requests\models.py", line 372, in prepare_url
enc_params = self._encode_params(params)
File "C:\Users\Administrator\PycharmProjects\Project\venv\python\lib\site-packages\requests\models.py", line 77, in _encode_params
for k, vs in to_key_val_list(data):
ValueError: too many values to unpack (expected 2)
报错代码:
def req(opt={}): """发起请求""" global res opt = opt if opt else {} opt["method"] = "POST" if bool(opt["method"]) == False else str(opt["method"]).upper() opt["url"] = opt["url"] if opt["url"] else '' opt["async"] = opt["async"] if opt["async"] else True opt["data"] = opt["data"] if opt["data"] else None opt["onsuccess"] = opt["onsuccess"] if opt["onsuccess"] else lambda: None try: opt["onerror"] = opt["onerror"] if opt["onerror"] else lambda: None except KeyError: pass params = [] headers = {} for k, v in dict(opt["data"]).items(): try: params.append(parse.urlencode(str(k)) + '=' + parse.urlencode(v)) except TypeError: params.append(k + "=" + v) # 判断提交方式 postData = '&'.join(params) if str(opt["method"]).upper() == "POST": for key, val in dict(opt["data"]).items(): headers[key] = val pass else: headers["Content-Type"] = ["application/x-www-form-urlencoded;charset=utf-8"] if str(opt["method"]).upper() == "POST": print(opt["data"]) print(headers) headers, opt["data"] = headers, {} res = requests.request(method=opt["method"], url=opt["url"], params=params, headers=headers) elif str(opt["method"]).upper() == "POST": res = requests.request("POST", opt["url"] + "?" + postData, opt["async"]) if res.status_code == 200 or res.status_code == 304: returner = opt["onsuccess"](res.text) if res.status_code != 200 and res.status_code != 304: raise AiRequestError("API请求失败或超时,请稍后再试!") return returner 请问怎么解决?
首页上一页 1 下一页尾页 3 条记录 1/1页