1.下載並引入requesthtml
cnpm install request --save;
const request = require("request");
2.基本使用jquery
request({ url: url,//請求路徑 method: "POST",//請求方式,默認爲get headers: {//設置請求頭 "content-type": "application/json", }, body: JSON.stringify(requestData)//post參數字符串 }, function(error, response, body) { if (!error && response.statusCode == 200) { } });
栗子:npm
1.抓取內容 var request = require('request'); request('https://www.baidu.com', function (error, response, body) { console.log(body)//打印百度首頁html內容 }) or request({url:'https://www.baidu.com'}, function (error, response, body) { console.log(body)//打印百度首頁html內容 }) 2.抓取文件 request('https://www.baidu.com/img/bd_logo1.png').pipe(fs.createWriteStream('a.png')) //抓取百度logo並保存到本地 fs.createReadStream('pic.jpg').pipe(request.put('http://veb.com/upload')) //讀取流pipe上傳
擴展:json
cheerio //npm install cheeriovar cheerio = require('cheerio'); var $ = cheerio.load('<ul id="fruits">...</ul>');//jquery 同樣的操做