Node.js爬虫如何获取天气和每日问候详解

安装node.js,同时安装好依赖:

npm install request --save // http请求库 npm install cheerio --save // 分析html工具 npm install express --save // nodejs web框架

获取墨迹天气

地址示例:

东莞 tianqi.moji.com/weather/chi

广州 tianqi.moji.com/weather/chi

深圳 tianqi.moji.com/weather/chi

可打开墨迹天气网站找到女朋友对应城市修改url地址拼音即可。

Node.js爬虫如何获取天气和每日问候详解

查看网页结构:

Node.js爬虫如何获取天气和每日问候详解

下面开始提取网页天气信息:

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风力:${win}\n${tips}` resolve(words) } else { reject(error) } }) }) }

获取每日一句

地址:wufazhuce.com/

Node.js爬虫如何获取天气和每日问候详解

const greetingURL = 'https://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] || '网络错误,获取每日问候失败!' resolve(result) } else { reject(error) } }) }) }

计算在一起天数

// 计算在一起的日子 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}天` }

接口调用

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("应用实例,访问地址为 %s:%s", port) })

Node.js爬虫如何获取天气和每日问候详解

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/8f1d9e056d58cc55d4fb376592ec89e9.html