Nodejs captures html page content

Source: Internet
Author: User
This article mainly introduces the key code for Nodejs to capture html page content, and also attaches nodejs to capture webpage content, which is very good for node. if you are interested in capturing page content in js, learn it together. javascript captures the core code of html page content.

The Code is as follows:

var http = require("http");var iconv = require('iconv-lite');var option = {hostname: "stockdata.stock.hexun.com",path: "/gszl/s601398.shtml"};var req = http.request(option, function(res) {res.on("data", function(chunk) {console.log(iconv.decode(chunk, "gbk"));});}).on("error", function(e) {console.log(e.message);});req.end();

Next, let's take a look at the following nodejs webpage capture content.

function loadPage(url) {var http = require('http');var pm = new Promise(function (resolve, reject) {http.get(url, function (res) {var html = '';res.on('data', function (d) {html += d.toString()});res.on('end', function () {resolve(html);});}).on('error', function (e) {reject(e)});});return pm;}loadPage('http://www.baidu.com').then(function (d) {console.log(d);});

For more articles about how Nodejs captures html pages, refer to the PHP Chinese website!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.