Javascript 在 Node.js / Express 中,如何“下载”页面并获取其 HTML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5801453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:55:50  来源:igfitidea点击:

In Node.js / Express, how do I "download" a page and gets its HTML?

javascripthttpnode.js

提问by TIMEX

Inside the code, I want to download "http://www.google.com" and store it in a string. I know how to do that in urllib in python. But how do you do it in Node.JS + Express?

在代码中,我想下载“http://www.google.com”并将其存储在一个字符串中。我知道如何在 python 中的 urllib 中做到这一点。但是你如何在 Node.JS + Express 中做到这一点呢?

采纳答案by Dve

Using node.js you can just use the http.request method

使用 node.js 你可以只使用 http.request 方法

http://nodejs.org/docs/v0.4.7/api/all.html#http.request

http://nodejs.org/docs/v0.4.7/api/all.html#http.request

This method is built into node you just need to require http.

此方法内置于节点中,您只需要要求 http。

If you just want to do a GET, then you can use http.get

如果你只想做一个 GET,那么你可以使用 http.get

http://nodejs.org/docs/v0.4.7/api/all.html#http.get

http://nodejs.org/docs/v0.4.7/api/all.html#http.get

var options = {
  host: 'www.google.com',
  port: 80,
  path: '/index.html'
};

http.get(options, function(res) {
  console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
  console.log("Got error: " + e.message);
});

(Example from node.js docs)

(来自 node.js 文档的示例)

You could also use mikeal's request module

你也可以使用 Mikeal 的 request 模块

https://github.com/mikeal/request

https://github.com/mikeal/request

回答by yojimbo87

var util = require("util"),
    http = require("http");

var options = {
    host: "www.google.com",
    port: 80,
    path: "/"
};

var content = "";   

var req = http.request(options, function(res) {
    res.setEncoding("utf8");
    res.on("data", function (chunk) {
        content += chunk;
    });

    res.on("end", function () {
        util.log(content);
    });
});

req.end();

回答by Natesh bhat

Simple short and efficient code :)

简单高效的代码:)

var request = require("request");

request(
    { uri: "http://www.sitepoint.com" },
    function(error, response, body) {
        console.log(body);
    }
);

doc link : https://github.com/request/request

文档链接:https: //github.com/request/request