安裝依賴
安裝node.js,同時安裝好依賴:
npm install request --save // http請求庫
npm install cheerio --save // 分析html工具
npm install express --save // nodejs web框架
獲取墨跡天氣
地址示例:
東莞 https://tianqi.moji.com/weather/china/Guangdong/dongguan
廣州 https://tianqi.moji.com/weather/china/guangdong/guangzhou
深圳 https://tianqi.moji.com/weather/china/guangdong/shenzhen
可打開墨跡天氣網(wǎng)站找到女朋友對應(yīng)城市修改url地址拼音即可则吟。
查看網(wǎng)頁結(jié)構(gòu):
下面開始提取網(wǎng)頁天氣信息:
const request = require("request")
const cheerio = require("cheerio")
const weatherURL = 'https://tianqi.moji.com/weather/china/Guangdong/dongguan'
// 獲取墨跡天氣提示信息
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=`今日${city}天氣\n${desc}\n溫度:${temp}\n濕度:${water}\n風(fēng)力:${win}\n${tips}`
resolve(words)
} else {
reject(error)
}
})
})
}
獲取每日一句
const greetingURL = 'http://wufazhuce.com/'
// 獲取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] || '網(wǎng)絡(luò)錯誤状勤,獲取每日問候失斄椭蟆懊悯!'
resolve(result)
} else {
reject(error)
}
})
})
}
計算在一起天數(shù)
// 計算在一起的日子
function getDiffDate(targetDate) {
let date1 = new Date(targetDate);
let date2 = new Date();
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 `寶貝,這是我們在一起的${diffDate}天`
}
接口調(diào)用
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 () { // 啟動ip和端口
var port = server.address().port
console.log("應(yīng)用實例桨昙,訪問地址為 http://%s:%s", port)
})