我在从Steam获取某商品的价格历史记录时遇到了麻烦。通过查看其他问题,我设法学习了一种构造链接的巧妙方法,该链接的确为我提供了商品的价格历史记录,我的问题是您必须登录Steam才能获取此数据。如何查看这些数据,就像我是通过http请求登录一样?我读过其他线程,他们在其中谈论浏览器会话以及在我这种情况下某人应如何设置会话ID的cookie,但我还没有设法使其在节点中正常工作。我得到的状态码是400。
这是我的代码:
const https = require('https');
const options = {
host: 'steamcommunity.com',
path: '/market/pricehistory/?country=SE¤cy=3&appid=730&market_hash_name=CS20%20Case',
header: {
'Set-Cookie': 'steamLogin=/*The session-id I got from writing "document.cookie"
in the dev-console while being logged in to Steam*/'
}
}
https.get(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));
我不确定代码中是否有错误或如何解决我遇到的这个问题。我非常感谢我能得到的任何帮助。
看来Steam删除了'steamLogin'cookie,从而解释了为什么去年有这么多人在其代码中使用它时遇到问题。相反,您想使用'steamLoginSecure'cookie。
首先,您需要登录https://steamcommunity.com。其次,您要查找“ steamLoginSecure” cookie并复制其中包含的内容。对于chrome来说是:
设置>高级>隐私和安全>站点设置> Cookies和站点数据>查看所有cookie和站点数据> steamcommunity.com> steamLoginSecure
现在复制'steamLoginSecure'的内容,并将其作为cookie放在标题中。
这是我最终得到的最终代码:
const https = require('https');
const options = {
host: 'steamcommunity.com',
path: '/market/pricehistory/?country=SE¤cy=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();