nodejs结合cheerio实现简单爬虫

 let cheerio = require("cheerio"),
fs = require("fs"),
util = require("util"),
html = "",
https = require('https'),
list = [],
buffer = null,
newslist = [],
url = 'https://www.yidaiyilu.gov.cn/';
let req = https.request("https://www.yidaiyilu.gov.cn/",function(res){
res.on("data",function(data){
list.push(data)
})
res.on("end",function(){
buffer = Buffer.concat(list)
html = buffer.toString()
$ = cheerio.load(html)
for(var i=1;i<=3;i++){
let dlist = `.con_yw_${i}`;
$(".mybox .main-1").find(dlist).find('a').each((index,ele)=>{
let txt = $(ele).text();
let alink = $(ele).attr("href")
let news = {};
news["title"] = txt;
news["url"] = url.substring(0,url.length-1)+alink;
newslist.push(news)
})
}
console.log(newslist)
})
})
req.end()

显示结果:

 [ { title: '中俄加快“一管两桥”建设 打通经贸合作加速发展                                                                              瓶颈',
url: 'https://www.yidaiyilu.gov.cn/xwzx/gnxw/93087.h tm' },
{ title: '“穗满俄”班列高附加值商品占比增高 中欧班列加 速中俄贸易纵深发展',
url: 'https://www.yidaiyilu.gov.cn/xwzx/gnxw/93099.h tm' },
{ title: '服务贸易激发“一带一路”合作潜能 知识密集型高 端服务出口成亮点',
url: 'https://www.yidaiyilu.gov.cn/xwzx/gnxw/93101.h tm' },
{ title: '中国与欧亚经济联盟成员国签署海关信息交换协定 ',
url: 'https://www.yidaiyilu.gov.cn/xwzx/gnxw/93103.h tm' },
{ title: '中外资银行助力“一带一路”走深走实',
url: 'https://www.yidaiyilu.gov.cn/xwzx/gnxw/93089.h tm' },
{ title: '满洲里口岸站中欧班列日接车数量达20列创历史新 高',
url: 'https://www.yidaiyilu.gov.cn/xwzx/gnxw/93131.h tm' },
{ title: '中蒙签署建设中蒙二连浩特—扎门乌德经济合作区 协议',
url: 'https://www.yidaiyilu.gov.cn/xwzx/gnxw/92906.h tm' },
{ title: '【越南】中企承建河内轻轨吉灵-河东线完成运营 演练',
url: 'https://www.yidaiyilu.gov.cn/xwzx/hwxw/93135.h tm' },
{ title: '【柬埔寨】中企承建“一带一路”西港热电工程正式 启动',
url: 'https://www.yidaiyilu.gov.cn/xwzx/hwxw/93105.h tm' },
{ title: '【俄罗斯】中国化学工程签订俄最大甲醇项目实施 协议 合同金额近15亿美元',
url: 'https://www.yidaiyilu.gov.cn/xwzx/hwxw/93095.h tm' },
{ title: '【巴基斯坦】巴媒:并非债务陷阱 中国是在挽救 巴经济',
url: 'https://www.yidaiyilu.gov.cn/xwzx/hwxw/92935.h tm' },
{ title: '【布隆迪】中国援布农业示范中心项目顺利通过中 期验收',
url: 'https://www.yidaiyilu.gov.cn/xwzx/hwxw/92923.h tm' },
{ title: '【匈牙利】中国品牌商品(中东欧)展在布达佩斯 举行',
url: 'https://www.yidaiyilu.gov.cn/xwzx/hwxw/92716.h tm' },
{ title: '【埃及】本班光伏产业园将在年内满负荷运行 中 国技术助力埃及建设“太阳能村”',
url: 'https://www.yidaiyilu.gov.cn/xwzx/hwxw/92726.h tm' },
{ title: '【俄罗斯】中俄首座跨境公路大桥合龙 俄官员: 造福两国人民的友谊之桥',
url: 'https://www.yidaiyilu.gov.cn/xwzx/hwxw/92724.h tm' },
{ title: '【四川】中欧班列(成都-莫斯科)运贸一体化班 列实现每月25列稳定开行',
url: 'https://www.yidaiyilu.gov.cn/xwzx/dfdt/93139.h tm' },
{ title: '【四川】再推8项出入境便利措施 参与“一带一路” 建设享加急办证',
url: 'https://www.yidaiyilu.gov.cn/xwzx/dfdt/93137.h tm' },
{ title: '【云南】中老铁路11.3公里长通达隧道将于明年上 半年贯通',
url: 'https://www.yidaiyilu.gov.cn/xwzx/dfdt/93133.h tm' },
{ title: '【北京】“一带一路”国际合作发展论坛在京举行'
url: 'https://www.yidaiyilu.gov.cn/xwzx/dfdt/93093.h tm' },
{ title: '【江西】探索成立“一带一路”中国瓷器之路旅游联 盟',
url: 'https://www.yidaiyilu.gov.cn/xwzx/dfdt/93091.h tm' },
{ title: '【河南】河南省印发实施口岸建设重点工作推进方 案',
url: 'https://www.yidaiyilu.gov.cn/xwzx/dfdt/92848.h tm' },
{ title: '【新疆】新疆“中欧班列集拼集运模式” 将在全国 复制推广',
url: 'https://www.yidaiyilu.gov.cn/xwzx/dfdt/92760.h tm' },
{ title: '【福建】福建加快“丝路”电商发展 增创外贸新优 势',
url: 'https://www.yidaiyilu.gov.cn/xwzx/dfdt/92756.h tm' } ]

nodejs实现简单爬虫的更多相关文章

  1. nodejs的简单爬虫

    闲聊       好久没写博客了,前几天小颖在朋友的博客里看到了用nodejs的简单爬虫.所以小颖就自己试着做了个爬博客园数据的demo.嘻嘻......      小颖最近养了条泰日天,自从养了我家 ...

  2. 用nodejs实现简单爬虫

    前言   本喵最近工作中需要使用node,并也想晋升为全栈工程师,所以开始了node学习之旅,在学习过程中, 我会总结一些实用的例子,做成博文和视频教程,以实例形式来理解体会node的用法,所以跟小猫 ...

  3. NodeJS简单爬虫

    NodeJS简单爬虫 最近一直在追火星的一本书,然后每次都要去网站看,感觉很麻烦,于是,想起用爬虫爬取章节,务实派,说干就干! 爬取思路 1.该网站的页面呈现出一定的规律 2.使用NodeJS的req ...

  4. 一次使用NodeJS实现网页爬虫记

    前言 几个月之前,有同事找我要PHP CI框架写的OA系统.他跟我说,他需要学习PHP CI框架,我建议他学习大牛写的国产优秀框架QeePHP. 我上QeePHP官网,发现官方网站打不开了,GOOGL ...

  5. Python简单爬虫入门三

    我们继续研究BeautifulSoup分类打印输出 Python简单爬虫入门一 Python简单爬虫入门二 前两部主要讲述我们如何用BeautifulSoup怎去抓取网页信息以及获取相应的图片标题等信 ...

  6. [Java]使用HttpClient实现一个简单爬虫,抓取煎蛋妹子图

    第一篇文章,就从一个简单爬虫开始吧. 这只虫子的功能很简单,抓取到”煎蛋网xxoo”网页(http://jandan.net/ooxx/page-1537),解析出其中的妹子图,保存至本地. 先放结果 ...

  7. 简单爬虫,突破IP访问限制和复杂验证码,小总结

    简单爬虫,突破复杂验证码和IP访问限制 文章地址:http://www.cnblogs.com/likeli/p/4730709.html   好吧,看题目就知道我是要写一个爬虫,这个爬虫的目标网站有 ...

  8. Python简单爬虫入门二

    接着上一次爬虫我们继续研究BeautifulSoup Python简单爬虫入门一 上一次我们爬虫我们已经成功的爬下了网页的源代码,那么这一次我们将继续来写怎么抓去具体想要的元素 首先回顾以下我们Bea ...

  9. GJM : Python简单爬虫入门(二) [转载]

    感谢您的阅读.喜欢的.有用的就请大哥大嫂们高抬贵手"推荐一下"吧!你的精神支持是博主强大的写作动力以及转载收藏动力.欢迎转载! 版权声明:本文原创发表于 [请点击连接前往] ,未经 ...

随机推荐

  1. 6.2 DataFrame

    一.DataFrame概述 在Spark SQL中,DataFrame就是它的数据抽象,对DataFrame进行转换操作. DataFrame的推出,让Spark具备了处理大规模结构化数据的能力,不仅 ...

  2. 7.Java基础_Java数据输入

    import java.util.Scanner; public class Output { public static void main(String[] args){ Scanner sc=n ...

  3. JS的节流、防抖及使用场景

    前言 据说阿里有一道面试题就是谈谈函数节流和函数防抖. 糟了,这可触碰到我的知识盲区了,好像听也没听过这2个东西,痛定思痛,赶紧学习学习.here we go! 概念和例子 函数防抖(debounce ...

  4. Spring熔断

    Hystrix不再维护,使用Resilience4j来代替.

  5. [C1W3] Neural Networks and Deep Learning - Shallow neural networks

    第三周:浅层神经网络(Shallow neural networks) 神经网络概述(Neural Network Overview) 本周你将学习如何实现一个神经网络.在我们深入学习具体技术之前,我 ...

  6. [C1W1] Neural Networks and Deep Learning - Introduction to Deep Learning

    第一周:深度学习引言(Introduction to Deep Learning) 欢迎(Welcome) 深度学习改变了传统互联网业务,例如如网络搜索和广告.但是深度学习同时也使得许多新产品和企业以 ...

  7. Python进阶-XVI 继承 单继承 多继承

    一.初识继承 1.引入继承 class A(object): pass # 父类,基类,超类 class B: pass # 父类,基类,超类 class A_son(A, B): pass # 子类 ...

  8. 首次使用gradle出现Could not find method leftShift() for arguments解决办法

    1.在win10桌面编写test.gradle脚本,里面内容如下 task helloword << { println 'Hello gradle qick start' } 2.在do ...

  9. 删除cookie的封装

    remove cookie(key,options){ options=options||{}; options.expires=-1; 删除cookie,其实就是修改cookie,将之前设置好的co ...

  10. scss和sass最大的区别

    重新接触了一下sass语法,在vscode的用的easysass插件.ctrl+S就可以自动编译成css文件.需要自己配置生成css路径 遇到的一个坑就是sass官网几乎全是写的sass,示例中全部是 ...