折腾:
【未解决】爬取mp.codeup.cn中的英语教材电子书资源
期间,希望搞清楚requests中,如何发送数据是application/x-www-form-urlencoded
requests application/x-www-form-urlencoded
好像是:
本身:
curHeaders["Content-Type"] = "application/x-www-form-urlencoded"
postDict = {
"ebookId": eachBookId
}
resp = requests.post(getAllPageUrl, headers=curHeaders, data=postDict)此处,数据就是x-www-form-urlencoded类型的。
否则如果是要json的字符串的话,则是:
curHeaders["Content-Type"] = "application/json"
postDict = {
"ebookId": eachBookId
}
resp = requests.post(getAllPageUrl, headers=curHeaders, data=postJsonStr)的写法了。
对此,可以借用
去调试验证
【总结】
最后用代码测试如下:
# Function: Demo requests
# Author: Crifan Li
# Update: 20200303
import json
import requests
postUrl = "http://httpbin.org/post"
postDataDict = {
"ebookId": "52365"
}
gContentTypeList = [
"application/x-www-form-urlencoded",
"application/json"
]
for curContentType in gContentTypeList:
print("%s post: %s %s" % ('-'*20, curContentType, '-'*20))
curHeaders = {
"Content-Type": curContentType
}
print("curHeaders=%s" % curHeaders)
if curContentType == "application/json":
curPostData = json.dumps(postDataDict)
else:
curPostData = postDataDict
print("curPostData=%s" % curPostData)
resp = requests.post(postUrl, headers=curHeaders, data=curPostData)
respText = resp.text
print("respText=\n%s" % respText)
"""
-------------------- post: application/x-www-form-urlencoded --------------------
curHeaders={'Content-Type': 'application/x-www-form-urlencoded'}
curPostData={'ebookId': '52365'}
respText=
{
"args": {},
"data": "",
"files": {},
"form": {
"ebookId": "52365"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "13",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0",
"X-Amzn-Trace-Id": "Root=1-5e5e5082-f5f8bff509e7f8a448c1e57a"
},
"json": null,
"origin": "117.82.228.217",
"url": "http://httpbin.org/post"
}
-------------------- post: application/json --------------------
curHeaders={'Content-Type': 'application/json'}
curPostData={"ebookId": "52365"}
respText=
{
"args": {},
"data": "{\"ebookId\": \"52365\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "20",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0",
"X-Amzn-Trace-Id": "Root=1-5e5e5084-b74e24a0898f623c8ee1411a"
},
"json": {
"ebookId": "52365"
},
"origin": "117.82.228.217",
"url": "http://httpbin.org/post"
}
"""
即可很清楚了上述逻辑了。
转载请注明:在路上 » 【已解决】Python中requests的POST发送类型application/x-www-form-urlencoded的数据