如何使用python api获取请求

问题描述 投票:0回答:1

我使用以下功能从网络获取数据,但失败。我想知道urllib.quote是否使用不正确

我使用了urllib.urlencode(xx),但显示为not a valid non-string sequence or mapping object

我的请求数据是:

[{"Keys": "SV_cY1tKhYiocNluHb", "Details": [{"id2": "PK_2gl9xtYKX7TJi29"}], "language": "EN", "id": "535985"}]

任何人都可以提供帮助。非常感谢!!!

###This Funcation call API Post Data
def CallApi(apilink, indata):
    token = gettoken()
    data = json.dumps(indata, ensure_ascii=False)
    print(data)
    headers = {'content-type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Bearer %s' % (token)}
    proxy = urllib2.ProxyHandler({})
    opener = urllib2.build_opener(proxy)
    urllib2.install_opener(opener)
    DataForGet=urllib.quote(data)
    NewUrl= apilink + "?" + DataForGet
    request = urllib2.Request(NewUrl, headers=headers)
    response = urllib2.urlopen(request, timeout=300)
    message = response.read()
    print(message)

错误:

以下错误消息:do_open引发URLError(err)中的文件“ /opt/freeware/lib/python2.7/urllib2.py”,行1198,>

我使用以下功能从网络获取数据,但失败。我想知道urllib.quote是否使用不正确,我是否使用过urllib.urlencode(xx),但它显示的不是有效的非字符串序列或映射...

python api get urllib urllib2
1个回答
0
投票

您可以看到urlencode

的评论]

将两元素元组的字典或序列编码为URL查询字符串

© www.soinside.com 2019 - 2024. All rights reserved.