通过节点http请求获取页面内容

getting the contents of a page through node http request

所以我正在尝试获取以下页面的内容:

http://steamcommunity.com/profiles/76561198009923867/inventory/json/730/2

所以我得到了这个代码:

app.get('/inventory', function(req, res){
//send a web request to http://www.steamcommunity.com/profiles/<NUM>/inventory
var options = {
    host: 'www.steamcommunity.com',
    port: 80,
    path: '/profiles/' + steamIDtoTrade + '/inventory/json/730/2/'
} 
http.get(options, function(http_res){
    var data = "";

    http_res.on("data", function(chunk){
        data += chunk;
    })

    http_res.on("end", function(){
        console.log(data);
        res.send(data);
    })
})

});

但是当我查看我的回复时,我得到的是:

http://i.imgur.com/IWb6ih8.png

那么我在这里错过了什么?

可能会尝试在您的 options 中添加 headers,如:

var options = {
    host: 'www.steamcommunity.com',
    port: 80,
    path: '/profiles/' + steamIDtoTrade + '/inventory/json/730/2/',
    headers: { 'Content-Type': 'application/json' }
} 

实际上刚刚结束了请求库。效果很好!

app.get('/inventory', function(req, res){
  var steamID = req.query.steamID;
  //send a web request to http://www.steamcommunity.com/profiles/<NUM>/inventory
  request({
    uri: 'http://www.steamcommunity.com/profiles/' + steamID + '/inventory/json/730/2/'
  }, function(error, response, body){
    res.send(body);
  })
});

你应该注意到,当你请求这个link“http://steamcommunity.com/profiles/76561198009923867/inventory/json/730/2”时,响应是:

HTTP/1.1 302 Moved Temporarily
Server: Apache
Content-Security-Policy: script-src 'self' 'unsafe-inline' 'unsafe-eval' http://steamcommunity-a.akamaihd.net/ https://api.steampowered.com/ http://www.google-analytics.com https://ssl.google-analytics.com https://www.google.com https://www.gstatic.com https://apis.google.com; object-src 'none'; connect-src 'self' https://steamcommunity.com http://steamcommunity.com https://api.steampowered.com/; frame-src 'self' http://store.steampowered.com/ https://store.steampowered.com/ http://www.youtube.com https://www.youtube.com https://www.google.com;
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Cache-Control: no-cache
Location: http://steamcommunity.com/id/QuickSkope/inventory/json/730/2
Vary: Accept-Encoding
Content-Type: text/html; charset=UTF-8
Content-Length: 26
Accept-Ranges: bytes
X-Varnish: 1414159326
Date: Wed, 11 Mar 2015 07:25:19 GMT
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Content-Encoding: gzip

所以真正的资源路径是“http://steamcommunity.com/id/QuickSkope/inventory/json/730/2”。 你应该知道 http 模块是一个非常简单的模块,它可以处理这种重定向情况。所以你可以使用这个库:https://github.com/olalonde/follow-redirects or request.

request 可以处理 http 重定向。

Request is designed to be the simplest way possible to make http calls. It supports HTTPS and follows redirects by default.

这样你就可以轻松使用stream了,

request.get("http://steamcommunity.com/profiles/76561198009923867/inventory/json/730/2").pipe(fs.createWriteStream("wy.txt"))