在尝试通过python请求发布数据时,出现了错误 来自浏览器检查控制台的实际表单数据:
{"params":"query=&hitsPerPage=1000&facetFilters=%5B%5B%22catalogs%3A000buyvallencom%22%5D%2C%22active%3Atrue%22%2C%22slug%3A3m-05710-superbuff-pad-adapter-p8hg1vv3b6b2%22%2C%22active%3Atrue%22%5D"}
我尝试了以下方法:
session=requests.session()
data={
"params":"query=&hitsPerPage=1000&facetFilters=%5B%5B%22catalogs%3A000buyvallencom%22%5D%2C%22active%3Atrue%22%2C%22slug%3A3m-05710-superbuff-pad-adapter-p8hg1vv3b6b2%22%2C%22active%3Atrue%22%5D"
}
response = session.post('https://lcz09p4p1r-dsn.algolia.net/1/indexes/ecomm_production_products/query?x-algolia-agent=Algolia%20for%20AngularJS%203.32.0&x-algolia-application-id=LCZ09P4P1R&x-algolia-api-key=2d74cf84e190a2f9cd8f4fe6d32613cc',data=data)
print(response.text)
但是在发布时,出现错误
{"message":"lexical error: invalid char in json text. Around 'params=que' near line:1 column:1","status":400}
答案 0 :(得分:1)
API接受JSON编码的POST数据。在您的帖子请求中将data=data
更改为json=data
。
从文档中
您也可以直接传递字典,而不是自己编码字典 使用json参数(在2.4.2版中添加),它将是 自动编码:
>>> url = 'https://api.github.com/some/endpoint' >>> payload = {'some': 'data'} >>> r = requests.post(url, json=payload)
请注意,如果传递了数据或文件,则会忽略json参数。
在请求中使用json参数将更改Content-Type 在application / json的标题中。
代码
import requests
session=requests.session()
url='https://lcz09p4p1r-dsn.algolia.net/1/indexes/ecomm_production_products/query?x-algolia-agent=Algolia%20for%20AngularJS%203.32.0&x-algolia-application-id=LCZ09P4P1R&x-algolia-api-key=2d74cf84e190a2f9cd8f4fe6d32613cc'
data={
"params":"query=&hitsPerPage=1000&facetFilters=%5B%5B%22catalogs%3A000buyvallencom%22%5D%2C%22active%3Atrue%22%2C%22slug%3A3m-05710-superbuff-pad-adapter-p8hg1vv3b6b2%22%2C%22active%3Atrue%22%5D"
}
response = session.post(url,json=data)
print(response.text)
输出
{"hits":[{"active":true,"heading":"05710 Superbuff Pad Adapter","heading_reversed":"Adapter Pad Superbuff 05710","subheading":"","features":"Our 3M™ Adaptors for Polishers are designed for saving time and hassle in collision repair jobs requiring double-sided screw-on compounding and polishing pads. It is part of a fast, effective assembly that incorporates our 3M™ Perfect-It™ Backup Pad and wool polishing pads, allowing users to quickly attach them without removing the adaptor. This durable adaptor is used with all polishers.<br>• Part of a complete assembly for compounding and polishing<br>• Designed to attach buffing pads or backup pads to machine polishers<br>• Helps reduce wear and vibration<br>• Users can change screw-on pads without removing the adaptor, saving time<br>• Provides hassle-free centering with 3M double-sided wool compounding and polishing pads","product_id":"P8HG1VV3B6B2","product_number":"IDG05114405710","brand":"","keywords":"sanding, polishing, buffing","image":"G8HI043XOD5X.jpg","image_type":"illustration","unspsc":"31191505","system":"sxe","cost":9.0039,"catalogs":["000BuyVallenCom"],"vendor":{"name":"3M","slug":"3m","vendor_id":"VACF1JS0AAP0","image":"G8HIP6V1J7UJ.jpg"},"taxonomy":{"department":{"name":"Paint, Marking & Tape","slug":"paint-marking-tape"},"category":{"name":"Filling, Polishing, Buffing","slug":"filling-polishing-buffing"},"style":{"name":"Adapters","slug":"adapters"},"type":{"name":"Pads","slug":"pads"},"vendor":{"name":"3M","slug":"3m"}},"slug":"3m-05710-superbuff-pad-adapter-p8hg1vv3b6b2","color":null,"material":null,"model":null,"model_number":null,"shape":null,"size":null,"display_brand":null,"style":null,"purpose":null,"product_type":null,"specifications":[],"item_specifications":[],"batch_id":"000BuyVallenCom-1551410144451","status":"Stk","erp":"05114405710","iref":null,"cpn":null,"gtin":"00051144057108","description":"05710 ADAPTOR 5/8 SHAFT SUPERBUFF","sequence":10,"item_id":"I8HG1VV6JL3B","vpn":"05710","uom":"Ea","specification_values":[],"objectID":"000BuyVallenCom-P8HG1VV3B6B2-I8HG1VV6JL3B","_highlightResult":{"heading_reversed":{"value":"Adapter Pad Superbuff 05710","matchLevel":"none","matchedWords":[]},"subheading":{"value":"","matchLevel":"none","matchedWords":[]},"brand":{"value":"","matchLevel":"none","matchedWords":[]},"taxonomy":{"style":{"name":{"value":"Adapters","matchLevel":"none","matchedWords":[]}}}}}],"nbHits":1,"page":0,"nbPages":1,"hitsPerPage":1000,"processingTimeMS":1,"exhaustiveNbHits":true,"query":"","params":"query=&hitsPerPage=1000&facetFilters=%5B%5B%22catalogs%3A000buyvallencom%22%5D%2C%22active%3Atrue%22%2C%22slug%3A3m-05710-superbuff-pad-adapter-p8hg1vv3b6b2%22%2C%22active%3Atrue%22%5D"}
文档