Unexpected character encountered while parsing value: l. Path , line 0, position 0.
该错误消息指出,在解析值时遇到了意外的字符“l”,这通常意味着服务器期望接收的数据格式与实际发送的数据格式不匹配。
-
参数格式不正确:确保你的
params
字典中的所有键和值都被正确地编码为URL编码的字符串。requests
通常会为你处理这个问题,但如果你手动构建了某些参数,它们可能需要被正确地编码。 -
复杂的数据结构:
application/x-www-form-urlencoded
格式不支持嵌套的数据结构。如果你的params
字典中包含了嵌套的字典或列表,你需要将它们转换为可以被URL编码的字符串。 -
JSON数据:如果服务器实际上期望接收JSON格式的数据,那么你需要将
Content-Type
更改为application/json
,并将params
转换为JSON字符串。
尝试如下操作:
import requests
import urllib.parse
url = "https://www.taizhou.gov.cn/tqy/DataListConfig/GetDataListHandler.ashx"
headers = {
# ...你的其他头部...
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
}
params = {
"a": 'init',
"_d": current_timestamp_ms,
"page": 1,
"limit": 20,
# ...其他参数...
}
# 将params字典转换为URL编码的字符串
encoded_params = urllib.parse.urlencode(params)
res = requests.post(url, data=encoded_params, headers=headers, verify=False)
if res.status_code == 200:
print(res.text)
else:
print(f"Error: {res.status_code}")
print(res.text) # 打印错误信息