nodejs爬虫

半腔热情 提交于 2020-01-16 04:11:46

1、npm init -y
2、在package.json
3、创文件夹app.js script下 “start”: “nodemon app.js”
4、下载nodemon npm i nodemon -S
5、下载 npm i cheerio -S
6、npmi request -S

var request = require(‘request’);
var cheerio = require(‘cheerio’);

// 通过 GET 请求来读取 http://cnodejs.org/ 的内容
request(‘https://baike.baidu.com/item/%E9%82%B3%E5%B7%9E%E9%93%B6%E6%9D%8F%E6%97%B6%E5%85%89%E9%9A%A7%E9%81%93/20185234?fr=aladdin’, function (error, response, body) {
if (!error && response.statusCode == 200) {
// 输出网页内容
// console.log(body);
var $ = cheerio.load(body);

var imgtu= $(’.body-wrapper’).find(’.content-wrapper’).find(’.side-content’).find(’.summary-pic’).find(‘a’).find(‘img’).attr(‘url’)
// $(‘h2.title’).text(‘Hello there!’);
// (h2).addClass(welcome);//console.log(('h2').addClass('welcome'); // console.log(.html());
console.log(imgtu)

}
});

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!