使用 Python 发出 ajax 请求未返回预期响应

发布于 2025-01-15 11:57:03 字数 3147 浏览 0 评论 0原文

尝试使用 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)

enter image description here

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏花。依旧 2025-01-22 11:57:03

由于您正在对 Javascript 进行 JSON 编码,因此您还需要对 Python 进行 JSON 编码:

import requests
res = requests.get(
    url='https://splintx.com/db.php',
    params={
        'card': json.dumps(
            {
                'id': 141,
                'foil': 0,
                'edition': 4,
            }
        )
    },
)

Since you are JSON-encoding your Javascript, you need to JSON-encode your Python as well:

import requests
res = requests.get(
    url='https://splintx.com/db.php',
    params={
        'card': json.dumps(
            {
                'id': 141,
                'foil': 0,
                'edition': 4,
            }
        )
    },
)
递刀给你 2025-01-22 11:57:03

经过进一步调试,我发现data: {card:priceData}实际上是作为表单数据发送的,而不是作为查询字符串发送的。 表单数据查询字符串之间的差异)< /a>
表单数据在 POST 请求的 HTTP 主体内发送,并借助
这个,我已经实现了下面的工作代码。

还不太清楚为什么在处理表单数据时应该预先创建Session

import requests

pricedata = '[{"id":141,"foil":0,"edition":4}]'
data = {'card': pricedata}

session = requests.Session()
req = session.post('https://splintx.com/db.php', data=data)

print(req.content)

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.

import requests

pricedata = '[{"id":141,"foil":0,"edition":4}]'
data = {'card': pricedata}

session = requests.Session()
req = session.post('https://splintx.com/db.php', data=data)

print(req.content)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文