版权声明:本文为 wintersmilesb101 -(个人独立博客– http://wintersmilesb101.online 欢迎访问)博主原创文章,未经博主允许不得转载。
var request = require("request");
var cheerio = require("cheerio");
request('http://news.163.com/',function(err,result){
if(err){
console.log("错误:"+err);
}
console.log(result.body);
})
npm install superagent --save
npm install superagent-charset
const install = require('superagent-charset');
const request = require('superagent');
superagent = install(request);
superagent.get('http://news.163.com/').charset('gb2312').end(function(err,res) {
if(err) console.log(err);
console.log(res.text);
});
npm install iconv-lite
var request = require('request');
var cheerio = require('cheerio');
var iconv = require('iconv-lite');
request.get({
url : 'http://temp.163.com/special/00804KVA/cm_yaowen.js?callback=data_callback',
encoding : null //让body 直接是buffer
}, function (err, response, body) {
//返回的body 直接就是buffer 了...
var buf = iconv.decode(body, 'gb2312');
console.log(buf);
});
data(
[
{
title:'Node.js爬虫爬坑之路',
date:'2017-03',
athour:'wintersmilesb101',
blog:'wintersmilesb101.online'
}
]
)
var request = require('request');
var iconv = require('iconv-lite');
request.get({
url : 'http://temp.163.com/special/00804KVA/cm_yaowen.js?callback=data_callback',
encoding : null //让body 直接是buffer
}, function (err, response, body) {
//返回的body 直接就是buffer 了...
var buf = iconv.decode(body, 'gb2312');
//去掉头部的 data_callback(
var str = buf.replace('data_callback(','');
//去掉尾部的 )
str = str.replace(')','');
//遍历输出数据,输出标题测试
var str1 = JSON.parse(str);
str1.forEach(function(s){
console.log(s.title);
});
});
var request = require('request');
var iconv = require('iconv-lite');
request.get({
url : 'http://temp.163.com/special/00804KVA/cm_yaowen.js?callback=data_callback',
encoding : null //让body 直接是buffer
}, function (err, response, body) {
//返回的body 直接就是buffer 了...
var buf = iconv.decode(body, 'gb2312');
//去掉头部的 data_callback(
var str = buf.replace('data_callback(','');
//去掉尾部的 )
str = str.replace(')','');
var str1 = JSON.parse(str);
str1.forEach(function(s){
console.log('文章标题:'+s.title);
console.log('摘要:'s.digest);
console.log('文章链接:'+s.docurl);
console.log('内容链接:'+s.commenturl);
console.log('tienum:'+s.tienum);
console.log('tlastid:'+s.tlastid);
console.log('tlink:'+s.tlink);
console.log('标签:'+s.label);
console.log('时间:'+s.time);
console.log('新闻类别:'+s.newstype);
console.log('频道名称:'+s.channelname);
console.log('图片链接:'+s.imgurl);
});
});