使用 Python 发出 ajax 请求未返回预期响应
尝试使用 python 在代码 (1) 中实现相同的请求,这是在线纸牌游戏纸牌查找网站 (https://splintx.com/)。预期响应应包含两个数据(日期数据和价格历史数据),如(2)中所示。
我如何使用 python 来模拟这个? 我尝试使用(3),但响应仅包含日期数据。它返回空的价格历史数据(请参阅(4))。 谢谢。
(1) 代码摘录 - Github
componentDidUpdate(prevProps, prevState) {
if (this.state.panel === 'forSale' && prevState.panel === 'multiSelect' && this.state.selected !== prevState.selected) {
this.updateSort('selected');
} else if (this.state.panel === 'history' && prevState.panel !== 'history') {
let edition = this.props.info.edition === 'Alpha' ? 0 : this.props.info.edition === 'Beta' ? 1 : this.props.info.edition === 'Promo' ? 2 : this.props.info.edition === 'Reward' ? 3 : this.props.info.edition === 'Untamed' ? 4 : 5;
let foilInt = this.props.info.gold ? 1 : 0;
let id = this.props.info.detailID;
let priceData = JSON.stringify([{
id: id,
foil: foilInt,
edition: edition
}]);
let storageSearch = 'history-' + priceData;
...
$.ajax({
type: 'POST',
url: 'https://splintx.com/db.php',
data: {card: priceData},
jsonpCallback: 'testing',
dataType: 'json',
success: function(history) {
let expiry = new Date();
expiry.setDate(expiry.getDate() + 1);
expiry.setUTCHours(0, 0, 0, 0);
let historyObj = {
expiry: expiry,
data: history
};
let storageName = 'history-' + priceData;
sessionStorage.setItem(storageName, JSON.stringify(historyObj));
let prices = history[1];
let dates = history[0];
let newPrices = [];
let newDates = [];
...
(2) 预期响应(在 Chrome Devtools->Application->Session Storage 上)
(3) Python 代码
import requests
res = requests.get(
url='https://splintx.com/db.php',
params={
'card':
{
'id': 141,
'foil': 0,
'edition': 4,
}
},
)
(4) 意外响应 - response[1]
应该有价格历史数据,但为空
[["2020-08-24 12:00:01am","2020-08-25 12:00:01am","2020-08-26 12:00:01am", ...], []]
Trying to make a same request implemented in Code (1) using python, which is a part of a backend data fetching process on an online card game card lookup website (https://splintx.com/). The intended response should contain two data (date data, and price history data) as shown in (2).
How could I simulate this using python?
I tried it with (3) but the response only contains date data. It returns the price history data empty (see (4)).
Thanks.
(1) Code excerpt - Github
componentDidUpdate(prevProps, prevState) {
if (this.state.panel === 'forSale' && prevState.panel === 'multiSelect' && this.state.selected !== prevState.selected) {
this.updateSort('selected');
} else if (this.state.panel === 'history' && prevState.panel !== 'history') {
let edition = this.props.info.edition === 'Alpha' ? 0 : this.props.info.edition === 'Beta' ? 1 : this.props.info.edition === 'Promo' ? 2 : this.props.info.edition === 'Reward' ? 3 : this.props.info.edition === 'Untamed' ? 4 : 5;
let foilInt = this.props.info.gold ? 1 : 0;
let id = this.props.info.detailID;
let priceData = JSON.stringify([{
id: id,
foil: foilInt,
edition: edition
}]);
let storageSearch = 'history-' + priceData;
...
$.ajax({
type: 'POST',
url: 'https://splintx.com/db.php',
data: {card: priceData},
jsonpCallback: 'testing',
dataType: 'json',
success: function(history) {
let expiry = new Date();
expiry.setDate(expiry.getDate() + 1);
expiry.setUTCHours(0, 0, 0, 0);
let historyObj = {
expiry: expiry,
data: history
};
let storageName = 'history-' + priceData;
sessionStorage.setItem(storageName, JSON.stringify(historyObj));
let prices = history[1];
let dates = history[0];
let newPrices = [];
let newDates = [];
...
(2) Intended response (on Chrome Devtools->Application->Session Storage)
(3) Python code
import requests
res = requests.get(
url='https://splintx.com/db.php',
params={
'card':
{
'id': 141,
'foil': 0,
'edition': 4,
}
},
)
(4) Unintended response - response[1]
is supposed to have price history data but is empty
[["2020-08-24 12:00:01am","2020-08-25 12:00:01am","2020-08-26 12:00:01am", ...], []]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您正在对 Javascript 进行 JSON 编码,因此您还需要对 Python 进行 JSON 编码:
Since you are JSON-encoding your Javascript, you need to JSON-encode your Python as well:
经过进一步调试,我发现
data: {card:priceData}
实际上是作为表单数据发送的,而不是作为查询字符串发送的。 (表单数据和查询字符串之间的差异)< /a>表单数据在 POST 请求的 HTTP 主体内发送,并借助 这个,我已经实现了下面的工作代码。
还不太清楚为什么在处理表单数据时应该预先创建Session。
After a further debugging, I found that the
data: {card: priceData}
is actually sent as Form Data, not as query string. (Differences between Form Data and query string)Form Data is sent inside the HTTP body of a POST request, and with the help of this, I have realised the below working code.
Not quite sure why Session should be created upfront when dealing with Form Data yet.