深圳燃气公司官网北京seo地址
深圳燃气公司官网,北京seo地址,群排名优化软件,wordpress怎么上传高清图片大小##用nodejs 写爬虫 关键模块#xff1a;cheerio、http#xff0c;request。 consonl.log() 出来的数据 首先安装相关的模块#xff1a; //我用了某宝的镜像安装 原来是 npm,-g 可以根据自己情况是全局按装还是局域安装
cnpm install -g jquerycnpm install -g cheerioc… ##用nodejs 写爬虫 关键模块cheerio、httprequest。 consonl.log() 出来的数据 首先安装相关的模块 //我用了某宝的镜像安装 原来是 npm,-g 可以根据自己情况是全局按装还是局域安装
cnpm install -g jquerycnpm install -g cheeriocnpm install requestcnpm install http
......复制代码根据你需要用的的模块安装 代码如下
// 新建一个对象
var MyUtil function () {
};
// var $ require(../node_modules/jQuery);
var request require(request);
// 用于 保存body的html数据
var bodtTemp;
MyUtil.prototype.getfunction(url,callback){// console.log(MyUtil.prototype) // 打印request(url, function (error, response, body) {if (!error response.statusCode 200) {// console.log(body) // 打印目标页面console.log(request );// 用临时变量保存起来请求回来的body数据bodtTempbody;var movie{} // movie.name $(body).find(span[propertyv:itemreviewed]).text();// movie.director $(body).find(#info span:nth-child(1) a).text();}})
}
// console.log(movie);
//获取目标网页的数据
var temp new MyUtil();
var httpUrlhttps://movie.douban.com/subject/25921812/?tag%E7%83%AD%E9%97%A8fromgaia_video;
// var httpUrlhttp://movie.douban.com/subject/1152952;
temp.get(httpUrl);
console.log(bodtTemp is bodtTemp);
// 开启自己的http服务器
var http require(http)
http.createServer(function(req,res){res.writeHead(200,{Content-Type:text/html});// res.write(bodtTemp);let cheerio require(cheerio)let $ cheerio.load(bodtTemp)// $(h2.title).text(Hello there!)// $(h2).addClass(welcome)// $(#db-nav-movie .nav-logo a).text(哈哈)// $(#dale_movie_subject_bottom_super_banner_frame).remove();// 移除 id 里面的内容// $(#footer).remove();console.log($(#info).text());// $.html()// console.log($.html());res.write($.html());// res.end(p结束/p);res.end();
}).listen(5858);复制代码保存为index.js 文件切换到 你的文件路径用node 命令打开 如 node index.js
复制代码浏览器打开 http://127.0.0.1:5858/
复制代码**记得要打开浏览器访问地址才能看到控制台的数据。 ###本文为了更好阅读以及新手可以直接执行提高新手兴趣故把所有可执行代码直接放一个文件详细以及一些测试也有备注。尝试去修改一下要爬的链接以及代码吧一切都是实践中的出效果。 转载于:https://juejin.im/post/5a3217baf265da4310486603
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/89464.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!