使用 node.js 获取 Steam 社区市场价格历史记录

Get Steam Community Market price history with node.js

我无法从 Steam 获取物品的历史价格。通过查看其他问题,我设法学会了一种构建 link 的巧妙方法,它确实为我提供了商品的价格历史记录,我的问题是您必须登录 Steam 才能获取此数据。如何查看这些数据,就像我通过 http 请求登录一样?我读过其他线程,他们讨论了浏览器会话以及我这种情况的人应该如何设置会话 ID 的 cookie,但我还没有设法让它在 node.js 中工作。我得到的状态代码是 400。

这是我的代码:

const https = require('https');

const options = {
  host: 'steamcommunity.com',
  path: '/market/pricehistory/?country=SE&currency=3&appid=730&market_hash_name=CS20%20Case',
  method: 'GET',
  headers: {
    'Cookie': `steamLoginSecure=THE SESSION ID I GOT FROM WRITING
              "document.cookie" IN THE DEV CONSOLE`
  }
}

const req = https.request(options, res => {
  console.log(res.statusCode);
  console.log(res.headers);

  let body = '';

  res.on('data', data => {
    body += data;
  });

  res.on('end', () => console.log(body));
}).on('error', error => console.log(error));
req.end();

我不确定我的代码是否有任何问题或如何解决我遇到的这个问题。我真的很感激我能得到的任何帮助。

似乎 Steam 已经删除了 'steamLogin' cookie,从而解释了为什么去年有这么多人在他们的代码中使用它时遇到问题。相反,您想使用 'steamLoginSecure' cookie。

首先您需要登录https://steamcommunity.com。其次,您要找到 'steamLoginSecure' cookie 并复制它包含的内容。对于 chrome 那将是:

设置 > 高级 > 隐私和安全 > 站点设置 > Cookie 和站点数据 > 查看所有 cookie 和站点数据 > steamcommunity.com > steamLoginSecure

现在复制 'steamLoginSecure' 的内容并将其作为 cookie 在您的 headers 中。

这是我得到的最终代码:

const https = require('https');

const options = {
  host: 'steamcommunity.com',
  path: '/market/pricehistory/?country=SE&currency=3&appid=730&market_hash_name=CS20%20Case',
  method: 'GET',
  headers: {
    'Cookie': 'steamLoginSecure=THE CONTENT OF "steamLoginSecure" HERE'
  }
}

const req = https.request(options, res => {
  console.log(res.statusCode);
  console.log(res.headers);

  let body = '';

  res.on('data', data => {
    body += data;
  });

  res.on('end', () => console.log(body));
}).on('error', error => console.log(error));
req.end();