常用URL内置方法
url.parse();
url.format()
url.resolve()
querystring
image.png
http/https小爬虫功能
var http = require('http');
var https = require('https');
var cheerio = require('cheerio') // 同jQuery插件
var url = 'url' // 要爬的网络地址
function filterMenu(html) {
var $ = cheerio.load(html)
var menu = $('.product-nav')
var menuData = []
menu.each(function(index, dom) {
var menuTitle = $(dom).find('li').text();
var menuLists = $(dom).find('a');
var menuList = []
// menuList.each(function(index, value) {
// menuList.push()
// })
menuData.push({
menTitle: menuTitle,
menuList: menuLists
})
})
return menuData
}
function printMenu(menu) {
menu.forEach(function (value) {
console.log(value.menTitle + '\n')
});
}
http.get(url, function(res) {
var html = '';
res.on('data', function(data) {
html += data;
});
res.on('end', function() {
// console.log(html)
var result = filterMenu(html);
printMenu(result);
})
res.on('error', function(err) {
console.log(err)
})
})
request GET功能
const https = require('https');
var options = {
hostname: 'spark.appublisher.com',
port: 443,
mrthod: 'GET',
path: '/course/get_open_class'
}
var responseData = '';
var request = https.request(options, (response) => {
// console.log(response);
// console.log(response.statusCode);
// console.log(response.headers);
response.setEncoding('utf8');
response.on('data', (chunk) => {
responseData += chunk;
})
response.on('end', () => {
JSON.parse(responseData).courses.map((item) => {
console.log(item.name)
})
})
})
request.on('error', (error) => {
console.log(error);
})
request.end();
request POST功能
const https = require('https');
const querystring = require('querystring')
var postData = querystring.stringify({
})
var options = {
hostname: 'spark.appublisher.com',
port: 443,
mrthod: 'POST',
path: '/course/get_open_class',
headers: {
}
}
var responseData = '';
var request = https.request(options, (res) => {
// console.log(response);
// console.log(response.statusCode);
// console.log(response.headers);
response.setEncoding('utf8');
response.on('data', (chunk) => {
responseData += chunk;
})
response.on('end', () => {
console.log('提交成功')
})
})
request.on('error', (error) => {
console.log(error);
})
request.write(postData)
request.end();
event事件功能
const EventEmitter = require('events');
class Player extends EventEmitter {}
var player = new Player()
player.on('play', (track) => {
console.log(`正在播放《${track}》`)
})
player.emit('play', '黑凤凰')
player.emit('play', '复仇者联盟4')