【问题标题】:getting the contents of a page through node http request通过节点http请求获取页面内容
【发布时间】:2015-03-11 04:12:45
【问题描述】:

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

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

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

【问题讨论】:

    标签: json node.js http steam


    【解决方案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);
      })
    });
    

    【讨论】:

      【解决方案2】:

      可以尝试在您的options 中添加headers,如下:

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

      【讨论】:

      • 不幸的是,这不起作用:(。如果它确实有帮助,我正在寻找的内容被包装在 HTML
         标记中。也许这与失真有关? 
      【解决方案3】:

      您应该注意到,当您请求此链接“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-redirectsrequest

      request 可以处理 http 重定向。

      Request 被设计成最简单的方式来使 http 来电。它支持 HTTPS 并默认遵循重定向。

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多