python接口调用失败_python3爬虫问题,API调用出现282004等错误

下面是一些错误:

"error_code": 282004,"error_msg": "invalid parameter(s)",a bytes-like object is required, not 'str'POST data should be bytes or an iterable of bytesContent-Length should be specified for iterable data of type {'text': '昆明会下雨吗?'}

等一系列都是因为数据格式的原因

1、Content-Length should be specified for iterable data of type

这里是因为request 参数不能dict或jsonpost_data = {"text":"昆明会下雨吗?"}

data=json.dumps(post_data).encode('GBK')

request = urllib.request.Request(url, data)

#JSON:在发起请求时,需要发送一个已经过编译的JSON数据:

data=json.dumps(post_data).encode('GBK')

2、a bytes-like object is required, not 'str'

POST data should be bytes or an iterable of bytes,这里是要提醒

注意区分bytes和str,需要格式转换。#bytes ⇒ str:str(b, encoding='****')response = urllib.request.urlopen(request)

content = response.read() # content是一个utf-8格式的

#bytes ⇒ str:str(b, encoding='****')

content_str = str(content, encoding="gbk")

3、参数错误,对照API接口查看body

"error_code": 282004, "error_msg": "invalid parameter(s)",# 用下一行是对的post_data ="{\"text\":\"昆明会下雨吗?\"}"

post_data = {"text":"昆明会下雨吗?"}

data=json.dumps(post_data).encode('GBK')

request = urllib.request.Request(url, data)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值