有没有python3的API例程
cheeryoung79 发布于2020-11 浏览:2018 回复:6
0
收藏

我用公有云APIpython 例程踩了不少坑,到现在也没出来。有没有python3的API例程?

收藏
点赞
0
个赞
共6条回复 最后由用户已被禁言回复于2020-11
#7用户已被禁言回复于2020-11
#5 cheeryoung79回复
还是报 not a valid non-string sequence or mapping object
展开

你这个错误不是接口返回的。你可以先找个懂Python的帮你看看。

百度接口不管成功还是失败都会返回json格式字符串内容。

1
#6用户已被禁言回复于2020-11
#5 cheeryoung79回复
还是报 not a valid non-string sequence or mapping object
展开

主要我不会Python  不知道哪个是Python2 哪个是Python3 

但是我发你的代码都是我亲测OK的。

不行你加我QQ 783021975 我给你看看。虽然不会Python但是用Python调用百度的接口是没问题的。

1
#5cheeryoung79回复于2020-11
#4 用户已被禁言回复
[代码]

还是报 not a valid non-string sequence or mapping object

0
#4用户已被禁言回复于2020-11
import urllib3, json, base64

def image_classification():
    #access_token获取方法请详见API使用说明,请注意access_token有效期为30天,这里仅为了简化编码每一次请求都去获取access_token
	access_token='【access_token】'
	http=urllib3.PoolManager()
    #请将API地址替换为EasyDL模型所提供的API地址
	url='【API地址】?access_token='+access_token
    #修改图片地址,测试不同图片
	f = open('【本地图片地址】','rb')
	#参数image:图像base64编码以及 threshold 阈值
	img = base64.b64encode(f.read())
	#img参数进行一下str转换
	params={'image':''+str(img,'utf-8')+'','threshold':0.5}
	#对参数params数据进行json处理
	encoded_data = json.dumps(params).encode('utf-8')
	print(encoded_data)
		request=http.request('POST', 
                      url,
                      body=encoded_data,
                      headers={'Content-Type':'application/json'})
	#对返回的byte字节进行处理。Python3输出位串,而不是可读的字符串,需要进行转换
	result = str(request.data,'utf-8')
	print(result)
#输出结果
print(image_classification())
1
#3cheeryoung79回复于2020-11

# encoding:utf-8
import urllib2

'''
easydl物体检测
'''

request_url = "【接口地址】"

 

params = "{\"image\":\"sfasq35sadvsvqwr5q...\"}"

access_token = '[调用鉴权接口获取的token]'
request_url = request_url + "?access_token=" + access_token
request = urllib2.Request(url=request_url, data=params)
request.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(request)
content = response.read()
if content:
print content

0
#2用户已被禁言回复于2020-11

具体哪个接口?

 

1
TOP
切换版块