问题描述:
近期写爬虫,发送请求时,返回同一个静态页面,检查之后是post请求里的参数格式不正确。因为data的参数直接从谷歌浏览器考下来,手动加引号改格式的。
form_data = {
'Data':'{
"appid": "123",
"checkin_type": "6",
"role": "1",
"stunum": "15812086122"
}'
'MsgType': 'APP_SEND_CARDCHECKIN_BYSTUNUM',
'refer':'xxx'
}
result = requests.post(url, data=form_data)
解决方案:
data = {
"appid": "123",
"checkin_type": "6",
"role": "1",
"stunum": "15812086122"
}
form_data = {
'Data': '%s' % data ,
'MsgType': 'APP_SEND_CARDCHECKIN_BYSTUNUM',
'refer':'xxx'
}
result = requests.post(url, data=form_data)
需提别注意的是,一定要用'Data': '%s' % info格式化输出这种形式把嵌套的字典传给Data,如果直接将info当做value赋值给Data,即,'Data': info,请求是不成功的
{'error_code': '505', 'error_msg': 'Data格式不正确参数验证失败'}
通常当post传入data参数时候,请求格式一般为Content-Type: application/x-www-form-urlencoded格式,按照官方的说法数据字典在发出请求时会自动编码为表单形式,所以是不是因为这个原因,data中嵌套的字典需要处理成字符串才能正常发post,这点暂存疑惑???
参考链接:https://blog.youkuaiyun.com/weixin_34593388/article/details/112845092
5543

被折叠的 条评论
为什么被折叠?



