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

时间:2015-03-11 04:12:46

标签: json node.js http steam

所以我试图获取以下页面的内容:

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

那我在这里错过了什么?

3 个答案:

答案 0 :(得分:1)

实际上刚刚结束了请求库。工作得很好!

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);
  })
});

答案 1 :(得分:0)

可能会尝试在headers中添加options,因为:

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

答案 2 :(得分:0)

您应该注意到,当您请求此链接&#34; http://steamcommunity.com/profiles/76561198009923867/inventory/json/730/2&#34;时,回复是:

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

所以真正的资源路径是&#34; http://steamcommunity.com/id/QuickSkope/inventory/json/730/2&#34;。 您应该知道http模块是一个非常简单的模块,它可以处理这种重定向情况。因此,您可以使用此库:https://github.com/olalonde/follow-redirectsrequest

request可以处理http重定向。

  

请求旨在成为制作http的最简单方法   调用。它支持HTTPS并默认遵循重定向。

所以你可以轻松使用流,

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