【Nodejs】cheerio简单示例
2024-08-25 09:47:42
cheerio的API挺多,我也了解有限,欲知详情请参考 “通读cheerio API”。
下面就事论事聊聊它的基本使用。
比如说在某网页中有这么一段HTML:
</tbody> <tbody id="stickthread_8349137" class="bs_bg1" > <tr> <td class="icon"> <a href="chat.php?tid=8349137" title="聊天模式" target="_blank"><img src="data:images/icons/icon6.gif" alt="Icon15" class="icon" /></a> </td> <th class="hot" > <label> <img src="data:images/2008/pin_1.gif" alt="本版置顶" title="本版置顶"/> </label> <em>[<a href="forumdisplay.php?fid=8&filter=type&typeid=48">看盘</a>]</em> <span id="thread_8349137" class="forumdisplay"><a href="thread-8349137-1-1.html" style="font-weight: bold;color: hotpink" target="_blank"> 2018年4月25日实时看盘交流 </a></span> <img src="data:images/attachicons/common.gif" alt="附件" title="附件" class="attach" /> <span class="threadpages"> <a href="thread-8349137-2-1.html">2</a> <a href="thread-8349137-3-1.html">3</a> <a href="thread-8349137-4-1.html">4</a> <a href="thread-8349137-5-1.html">5</a> <a href="thread-8349137-6-1.html">6</a> .. <a href="thread-8349137-14-1.html">14</a> </span> </th> <td class="author"> <cite> <a href="space.php?action=viewpro&uid=2713715">美人鱼苗苗</a> </cite> <em class="ad_hong" >2018-4-24</em> </td> <td class="nums"><strong>267</strong><em>4911</em></td> <td class="lastpost"> <cite><a href="space.php?action=viewpro&username=%D6%F1%D4%B0%C7%E5">竹园清</a></cite> <em><a href="redirect.php?tid=8349137&goto=lastpost#lastpost"><font class="ad_hong">今天 20:33</font></a></em> </td> </tr> </tbody>
注意上面代码中加粗加下划线的三个部分,它们是:
thread-8349137-1-1.html
2018年4月25日实时看盘交流
14
这三个量分别对应了帖子的地址,标题和共多少页,如果要用cheerio取到它们该如何呢,请见代码:
var buffer = Buffer.concat(html); var body = iconv.decode(buffer,'gb2312'); var $ = cheerio.load(body); // 这个$是整个网页的dom $("tbody").each(function(index,element){ // 先找到tody节点 var $tbody=cheerio.load($(element).html()); var topic={}; topic.pageCount=1; topic.url=null; topic.title=null; $tbody(".forumdisplay a").each(function(index,element){ // 再找tbody节点里的class=forumdisplay里面的链接 var topicUrl='http://www.55188.com/'+$tbody(element).attr("href"); // 得到链接的属性(第一项) var topicTitle=$tbody(element).text();// 得到链接的文字(第二项) topic.url=topicUrl topic.title=topicTitle; }) $tbody(".threadpages").each(function(index,element){ // 再找tbody节点里的class=threadpages节点 topic.pageCount=$tbody(element).children().last().text();// 找到最后一个子节点的文字(第三项) }) if(topic.url!=null && topic.title!=null){ topics.push(topic); // 加入数组 } })
这样就找到了需要的三个值。
最新文章
- jquery实现ajax提交表单信息
- 【C#进阶】override new virtual
- 注册Jdbc驱动程序的三种方式
- Unity路径规划
- Spring中@Async用法总结
- 《SQL Server企业级平台管理实践》读书笔记——几个系统库的备份与恢复
- Flash相关知识
- URLDecoder与URLEncoder
- js中的继承1--类继承
- 【转载】图论 500题——主要为hdu/poj/zoj
- HTML5学习(九)----应用程序缓存
- ssh远程连接不上ubuntu
- setTimeout()与setInterval()方法区别介绍
- 利用linux BT5来破解无线 破解无线
- vue+vux+axios+vuex+vue-router的项目的理解
- Go 语言打包静态文件
- 【NOIP 2017】宝藏
- 线程中的samaphore信号量及event事件
- codeforces 1051F The Shortest Statement
- 提示 make: 没有什么可以做的为 `all&#39;
热门文章
- 模型构建<;1>;:模型评估-分类问题
- 【推导】【贪心】Codeforces Round #472 (rated, Div. 2, based on VK Cup 2018 Round 2) D. Riverside Curio
- 502 解决:[WARNING] fpm_children_bury
- hdu 2121
- 2018-2019-20172329 《Java软件结构与数据结构》第三周学习总结
- Codeforces Round #248 (Div. 1) A. Ryouko&#39;s Memory Note 水题
- Charles基本使用—http抓包、手机抓包、数据篡改
- ROWID面试题-删除表中重复数据(重复数据保留一个)
- js面向对象编程-高级内容
- CentOS 6.9配置EPEL源