在Node.js / Express中,我如何“下载”一个页面并获取其HTML? [英] In Node.js / Express, how do I "download" a page and gets its HTML?
问题描述
在代码中,我想下载http://www.google.com并将其存储在字符串中。
我知道如何在python中的urllib中这样做。但是你如何在Node.JS + Express中做到这一点?
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?
推荐答案
使用node.js你可以使用http.request方法
Using node.js you can just use the http.request method
http://nodejs.org/docs/v0.4.7/api/all.html#http.request
此方法内置于节点中只需要要求http。
This method is built into node you just need to require http.
如果你只是想做GET,那么你可以使用http.get
If you just want to do a GET, then you can use 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);
});
(来自node.js docs的示例)
(Example from node.js docs)
你也可以使用mikeal的请求模块
You could also use mikeal's request module
https:/ /github.com/mikeal/request
这篇关于在Node.js / Express中,我如何“下载”一个页面并获取其HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!