node reptiles

axios
(async function(){
	let data=await axios.get('接口');
	console.log(data)
})()
request
const request=require('request');
const fs=require('fs');
let url='https://juejin.im/welcome/frontend';
request(url,function(err,response,body){
	fs.writeFileSync(body.html,body);
	let reg=/st:name="title" class="title" data-v-\w+>(.+?)<\/a>/g;
	let titles=[];
	body.replace(reg,function(match,title){
		titles.push(title);
	})
	console.log(titles)
})
puppeteer
  • Screenshots
let puppeteer=require('puppeteer');
(async function(){
	const browser=await puppeteer.launch({headless:false});//启动浏览器
	const page=await browser.newPage();//打开一个标签
	await page.goto('http://www.baidu.com');//跳转到百度
	await page.screenshot({
		path:'baidu.png',
		type:'png'
	})
	//browser.close();
})()
  • reptile
(async function(){
	const browser=await puppeteer.launch({headless:false});//启动浏览器
	let page=await browser.newPage();
	await page.goto('https://juejin.im/welcome/frontend');
	const titles=await page.$$eval('a.title',function(as){        //获取<a class="title"></a>元素标题
		return Array.from(as).map(a=>a.innerText)
	})
	console.log(titles)
})()
cheerio
  • attr
  • removeAttr
  • prop
    $(“input[type=‘checkbox’]”).prop(“checked”);
  • data()
    $("<div data-apple-color=‘red’>").data(); //{apple-color:‘red’}
    $("<div data-apple-color=‘red’>").data(‘apple-color’); //red
  • val()
  • hasClass()
  • addClass
  • removeClass
  • find
  • parent
  • next
  • nextAll
  • prev
  • prevAll
  • slice
  • siblings
  • first
  • last
  • eq
  • children
  • each
  • map
  • filter
  • after
  • before
  • append
  • remove
  • replaceWith
  • empty
  • html
  • text
//通过request请求回来的html代码,通过var $=cheerio.load(html),接下来就可以通过$(selector)
cron (timing)

Parameters representing:

unit range
seconds 0-59
minutes 0-59
hours 0-23
day 1-31
months 0-11
week 0-6

Here you can write a particular value or value range 1-5 (represented by 1-5 seconds, once every second)

  • Per second
const cron=require('cron').CronJob;
//每秒
let job=new cron('* * * * * *',function(){
	console.log(new Date().toLocaleTimeString())
})
//1,3,5,7,9秒,每秒执行一次
let job1=new cron('1,3,5,7,9 * * * * *',function(){
	console.log(new Date().toLocaleTimeString())
})
//1-5秒每秒执行一次
let job2=new cron('1-5 * * * * *',function(){
	console.log(new Date().toLocaleTimeString())
})
//每隔5秒执行一次
let job3=new cron('*/5 * * * * *',function(){
	console.log(new Date().toLocaleTimeString())
})
//只有1秒执行一次
let job=new cron('1 * * * * *',function(){
	console.log(new Date().toLocaleTimeString())
})
job.start()
pm2

pm2 is a process manager, by pm2 start + file, you can start the corresponding process

iconv-lite (or converted to utf-8 GBK)

var iconv = require ( 'iconv- lite');
through the request to take back the body
body iconv.decode = (body, 'GBK');
this time on all normal body

nodemailer (send mail)
const nodemailer=require('nodemailer');

const transport=nodemailer.createTransport({
	service:'qq',
	port:465,
	secureConnection:true,
	auth:{
		user:'[email protected]',
		pass:'eppemjmncaagbggf'
	}
})
let options={
	from:'[email protected]',
	to:'[email protected]',
	subject:'hello',
	html:'<h1>hello</h1>'
}
transport.sendMail(options)

Guess you like

Origin blog.csdn.net/qq_33332184/article/details/91820340