nodejs 采集新闻数据

使用nodejs采集新华新闻数据


代码段:

var express = require('express');
var $ = require('jQuery');
var app = express();
var colors = require('colors');
var message_list = require('./zui/message_list');
var findData = require('./findData');

//设置全局跨域访问  
app.all('*', function(req, res, next) {  
    res.header("Access-Control-Allow-Origin", "*");  
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");  
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");  
    res.header("X-Powered-By",' 3.2.1')  
    res.header("Content-Type", "application/json;charset=utf-8"); 
    next();  
});

//输出主页列表数据
app.get('/getIndexData', function (req, res) {
	findData.indexData(req, res);
});


/*
*输出列表数据。支持范围选择
*demo:http://localhost:3000/getList/0,9 前9条数据 从0开始
*demo: http://localhost:3000/getList/0  所有的数据  从0开始
*/
app.get(/^\/getList?(?:\/(\d+)(?:\,(\d+))?)?/, function (req, res) {
	var start=undefined,end=undefined;
	if(req.params.length==1){
		start=req.params[0];
	}else if(req.params.length==2){
		start=req.params[0];
		end=req.params[1];
	}
	findData.list(req, res,start,end,function(data){
		for(var i=0,j=data.length;i<j;i++){
			data[i].orderby="1";
		}
		return data;
	});
});

//数据列表
var newsListData=undefined;
app.get('/getNewsList', function (req, res) {
	if(typeof newsListData=="undefined"){
		findData.list(req, res,undefined,undefined,function(data,length){
			for(var i=0,j=data.length;i<j;i++){
				data[i].orderby=Math.round(Math.random()*6);
				data[i].summary="暂无摘要";
				data[i].updateUserName="admin";

			}
			newsListData=data;
			getNewsList(req, res);
		});
	}else{
		getNewsList(req, res);
	}
});

//缓存数据列表
function getNewsList(req, res){
	var pageNum=req.query.pageNum,pageSize=req.query.pageSize;
	var start=(pageNum-1)*pageSize,end=pageNum*pageSize;
	var newData=newsListData.slice(start, end);
	res.send(JSON.stringify(newData));
}

app.listen(3000);
console.log("nodejs was start".green);


var http = require('http'),
	fs = require('fs'),
	jquery = fs.readFileSync("lib/jquery.min.js", "utf-8"),
	jsdom = require('jsdom');

function get(url, callback) {
	//使用代理
        var opt = {
		host: '127.0.0.1',
		port: '7070',
		method: 'get', //这里是发送的方法
		path: url
	}
	//以下是接受数据的代码
	var req = http.request(opt, function(res) {
		res.setEncoding('utf8');
		var html = '';
		res.on('data', function(d) {
			html += d;
		}).on('end', function() {
			jsdom.env({
				html: html,
				src: [jquery],
				done: function(errors, window) {
					var $ = window.$;
					callback && callback(errors, $);
					window.close(); // 释放window相关资源,否则将会占用很高的内存
				}
			});
		});
	}).on('error', function(e) {
		console.log("Got error: " + e.message);
	})
	req.end();
}

//导出list
exports.indexData = function(request, response, start, end) {
	get('http://www.news.cn/edu/index.htm', function(errors, $) {
		var $list = $(".list");
		var data = [];
		$list.each(function(index) {
			var $this = $(this),
				$li = $this.find("li"),
				li_data = [];
			$li.each(function(index) {
				var $a = $(this).find("a");
				var href=$a.attr("href").replace("http://news.xinhuanet.com/edu/","");
				var temp_href="/"+href.replace(/[^c]*/,"");
				var date=href.replace(temp_href,"").replace("/","-");
				if(date.length>10){
					date="2013-01-04";
				}

				li_data.push({
					title: $a.text(),
					href: $a.attr("href"),
					date:date,
					id: index
				});
			});
			data.push({
				id: index,
				list: li_data
			});
		});

		if (typeof start != "undefined" && typeof end == "undefined") {
			data = data.slice(start);
		} else if (typeof start != "undefined" && typeof end != "undefined") {
			data = data.slice(start, end);
		}
		response.send(JSON.stringify(data));
	});
}

//导出list
exports.list = function(request, response, start, end,callback) {
	get('http://www.news.cn/edu/index.htm', function(errors, $) {
		var $li = $(".list li");
		var data = [];
		$li.each(function(index) {
			var $a = $(this).find("a");
			var href=$a.attr("href").replace("http://news.xinhuanet.com/edu/","");
			var temp_href="/"+href.replace(/[^c]*/,"");
			var date=href.replace(temp_href,"").replace("/","-");
			if(date.length>10){
				date="2013-01-04";
			}
			
			data.push({
				id: index,
				title: $a.text(),
				attachment: $a.attr("href"),
				updateTime:date
			});
		});

		var newData=data;
		if (typeof start != "undefined" && typeof end == "undefined") {
			newData = data.slice(start);
		} else if (typeof start != "undefined" && typeof end != "undefined") {
			newData = data.slice(start, end);
		}
		if(typeof callback=="function"){
			newData=callback(newData,data.length);
		}
		//response.header("Access-Control-Allow-Origin", "*");   //设置跨域访问  
		response.send(JSON.stringify(newData));
	});
}

访问地址

http://localhost:3000/getIndexData
http://localhost:3000/getList/0,4
http://localhost:3000/getNewsList?pageNum=1&pageSize=10

over

要在Node.js中修改JSON数据,您可以使用以下步骤: 1. 请确保您已经安装了Node.js和相关的模块(比如fs模块)。 2. 使用fs模块的readFile函数读取JSON文件的内容,并将其转换为JavaScript对象。 3. 修改JavaScript对象中的属性值。 4. 使用JSON.stringify函数将修改后的JavaScript对象转换回JSON字符串。 5. 使用fs模块的writeFile函数将修改后的JSON字符串写入到JSON文件中。 根据您提供的代码片段,可以使用以下方法来修改JSON文件中的数据: 首先,将JSON文件的内容读取到一个JavaScript对象中: ```javascript const fs = require('fs'); const filename = './data/Details_shoplist_info.json'; const content = fs.readFileSync(filename, 'utf8'); const obj = JSON.parse(content); ``` 然后,根据您的需求修改JavaScript对象中的属性值。例如,要将`item`属性的值修改为`param`,可以使用以下代码: ```javascript obj.message = param; ``` 接下来,将修改后的JavaScript对象转换回JSON字符串: ```javascript const result = JSON.stringify(obj); ``` 最后,使用fs模块的writeFile函数将修改后的JSON字符串写入到JSON文件中: ```javascript fs.writeFile(filename, result, 'utf8', function (err) { if (err) { console.error(err); } else { console.log("JSON文件修改成功!"); } }); ``` 这样就完成了在Node.js中修改JSON数据的过程。请注意,这只是一种示例方法,您可以根据实际需求进行调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [在 nodeJs 中修改 json 文件中的数据](https://blog.csdn.net/weixin_44592813/article/details/102566191)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [nodeJS文件检索,获取JSON文件内容,修改指定JSON节点内容](https://blog.csdn.net/weixin_45438335/article/details/128456903)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值