当"Content-Type":"application/x-www-form-urlencoded时,data就是soCode=xxx,
但是python3会抱错POST data should be bytes or an iterable of bytes. It cannot be of type str.,通过json.dumps(data).encode(encoding='utf-8')转化成bytes之后,实际post的data会变成“soCode=xxx”,怎么重新变成soCode=xxx?
当 Content-Type: application/json
post data这样写:
{"参数名":"参数值","参数名":"参数值",...}
{'soCode':'xxx'}
当Content-Type: application/x-www-form-urlencoded
post data这样写:
name1=valule1&name2=value2...
'soCode=xxx'
例子:
百度翻译:
url='https://fanyi.baidu.com/sug'
headers={
'Host':'fanyi.baidu.com',
'Origin':'https://fanyi.baidu.com',
'Referer':'https://fanyi.baidu.com/?aldtype=16047',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.15 Safari/537.36',
'X-Requested-With':'XMLHttpRequest',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
}
data='kw=love'
req=requests.post(url=url,headers=headers,data=data).text
print(req)