Install dependencies

Install Node.js and install dependencies:

npm install request --save // HTTP request library
npm install cheerio --save // Analyze HTML tools
npm install express --save // Nodejs Web framework
Copy the code

Get ink weather

Address example: dongguan tianqi.moji.com/weather/chi… Guangzhou tianqi.moji.com/weather/chi… Shenzhen tianqi.moji.com/weather/chi…

Can open the ink weather website to find girlfriend corresponding city to modify the URL address pinyin.

View page structure:

const request = require("request")
const cheerio = require("cheerio")
const weatherURL = 'https://tianqi.moji.com/weather/china/Guangdong/dongguan'

// Get ink weather information
function getWeatherTips(url) {
    return new Promise((resolve,reject) = >{
    	request(weatherURL,(error,res,body)=>{
    	  if(! error) {let html = res.body || "";
    		let $ =cheerio.load(html)
    		let temp = $('.wea_weather em').text().trim()+'℃'
    		let desc = $('.wea_weather b').text().trim()
    		let water = $('.wea_about span').text().trim()
    		let win = $('.wea_about em').text().trim()
    		let tips = $('.wea_tips em').text().trim()
    		let words=` today${city}The weather \ n${desc}\ n temperature:${temp}Humidity \ n:${water}Wind: \ n${win}\n${tips}`
    		resolve(words)
    	} else {
    		reject(error)
    	}      
      })
   })   
}

Copy the code

Get a sentence of the day

Address: wufazhuce.com/

const greetingURL = 'http://wufazhuce.com/'
// Get a sentence of the day at wufazhuce.com
function getDailyGreeting() {
    return new Promise((resolve,reject) = >{
    	request(greetingURL,(error,res,body)=>{
    		let everyDayWordsList = [];
    		if(! error && res.statusCode==200) {
    			let $ = cheerio.load(res.body.toString())
    				$('div .fp-one-cita a').each(function(item){
    					if($(this).text()){
    						everyDayWordsList.push($(this).text().trim())
    					}           
    				})
    			let result = everyDayWordsList[0] | |'Network error, failed to get daily greetings! '
    			resolve(result)    
    		} else {
    			reject(error)				
    		}
    	})
    })
}

Copy the code

Count the days together

// Count the days together
function getDiffDate(targetDate) {
    let date1 = new Date(targetDate);
    let date2 = new Date(a); date1 =new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());
    date2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());
    const diff = date2.getTime() - date1.getTime();
    const diffDate = diff / (24 * 60 * 60 * 1000);
    return 'Baby, this is us together${diffDate}Day `
}
Copy the code

Interface call

var express=require('express');
var app=express();
const api = require('./crawer.js')
app.get('/words',async function(req,res){
   let dateTips = api.getDiffDate()
   let weather  = await api.getWeatherTips()
   let words    = await api.getDailyGreeting()
   let str =`${dateTips}\n${weather}\n${words}`
   res.writeHead(200,  {'Content-Type': 'application/json; charset=utf-8'})
   res.end(str)
})
var server = app.listen(8083, function() {// Start IP and port var port = server.address().port console.log()"Application instance, access at http://%s:%s", port)
})
Copy the code