1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > node.js基础模块http 网页分析工具cherrio实现爬虫【node.js】

node.js基础模块http 网页分析工具cherrio实现爬虫【node.js】

时间:2022-12-18 21:42:41

相关推荐

node.js基础模块http 网页分析工具cherrio实现爬虫【node.js】

web前端|js教程

node.js基础模块http,网页分析工具cherrio,node.js爬虫

web前端-js教程

A、前言

说是爬虫初探,其实并没有用到爬虫相关第三方类库,主要用了node.js基础模块http、网页分析工具cherrio。 使用http直接获取url路径对应网页资源,然后使用cherrio分析。 这里我主要学习过的案例自己敲了一遍,加深理解。在coding的过程中,我第一次把jq获取后的对象直接用forEach遍历,直接报错,是因为jq没有对应的这个方法,只有js数组可以调用。

平衡二叉树 源码,vscode自定义模板变量,ubuntu系统安装分区,tomcat启动很多乱码,sqlite 去掉空格,网页设计使用方法,阿里云服务器主机,html 视频插件,django集成前端框架,scrapy启动爬虫,php弹幕,seo手机流量,springboot功能插件化,php蓝色大气科技企业网站,psd格式网页,橱柜企业网站模板,网站建好了 怎么建后台,扁平化后台页面,java管理系统代码,小程序介绍怎么写知乎lzw

B、知识点

①:superagent抓去网页工具。我暂时未用到。

②:cherrio 网页分析工具,你可以理解其为服务端的jQuery,因为语法都一样。

效果图

定位系统源码,vscode输出窗自动刷新,怎么访问ubuntu,查看tomcat的进程,.netcore爬虫框架,优酷视频源码php,正规seo推广公司有哪些,网站编辑工具,阿里巴巴官网模板lzw

1、抓取整个网页

企业官网单页源码,ubuntu 16内核版本,tomcat端口号有哪些,pytv爬虫教程,php处理高并发大流量,seo查询服务lzw

2、分析后的数据,提供的示例为案例实现的例子。

爬虫初探源码分析

var http=require(http);var cheerio=require(cheerio); var url=/learn/348; /****************************打印得到的数据结构[{ chapterTitle:\, videos:[{ title:\, id:\ }]}]********************************/function printCourseInfo(courseData){ courseData.forEach(function(item){ var chapterTitle=item.chapterTitle; console.log(chapterTitle+\ ); item.videos.forEach(function(video){ console.log( 【+video.id+】+video.title+\ ); }) });} /*************分析从网页里抓取到的数据**************/function filterChapter(html){ var courseData=[]; var $=cheerio.load(html); var chapters=$(.chapter); chapters.each(function(item){ var chapter=$(this); var chapterTitle=chapter.find(strong).text(); //找到章节标题 var videos=chapter.find(.video).children(li); var chapterData={ chapterTitle:chapterTitle, videos:[] }; videos.each(function(item){ var video=$(this).find(.studyvideo); var title=video.text(); var id=video.attr(href).split(/video)[1]; chapterData.videos.push({ title:title, id:id }) }) courseData.push(chapterData); }); return courseData;} http.get(url,function(res){ var html=\; res.on(data,function(data){ html+=data; }) res.on(end,function(){ var courseData=filterChapter(html); printCourseInfo(courseData); })}).on(error,function(){ console.log(获取课程数据出错);})

参考资料:

/alsotang/node-lessons/tree/master/lesson3

/video/7965

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。