赞
踩
npm i request -D
npm i iconv-lite -D
npm i cheerio -D
const fs = require('fs'); const iconv = require("iconv-lite");//解决编码格式 const request = require("request");//处理请求 const cheerio = require("cheerio");//同jQuery 可以使用$选择器 request('https://v.qq.com/biu/ranks/?t=hotplay&channel=all#anime', { encoding: null }, function (error, response, body) { if (error) throw error; console.log('爬取成功...') let str = iconv.decode(body, 'utf-8')//bufer转码 请求过来的网页 // console.log(str) const $ = cheerio.load(str); let arr = []; //分析dom结构 抓取数据 for (let i = 1; i <= 2; ++i) { for (let j = 1; j <= 3; ++j) { let selImg1 = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hot_first > a > img` let selHref1 = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hot_first > a` let selText = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hot_first > strong` let selDes = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hot_first > div.figure_desc` let obj = { imgSrc: $(selImg1).attr('src'), href: $(selHref1).attr('href'), text: $(selText).text(), des: $(selDes).text(), brother: [], } for (let k = 1; k <= 4; ++k) { let broSelImg1 = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hotlist > ol > li:nth-child(${k}) > a > img` let broSelHref1 = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hotlist > ol > li:nth-child(${k}) > a` let broSelText1 = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hotlist > ol > li:nth-child(${k}) > strong` let broDes = `#app > div > div > div.mod_row_box > div > div:nth-child(${i}) > div:nth-child(${j}) > div.mod_rank_list > div.mod_hotlist > ol > li:nth-child(${k}) > div` let broObj = { imgSrc: $(broSelImg1).attr('src'), href: $(broSelHref1).attr('href'), text: $(broSelText1).text(), des: $(broDes).text() } obj.brother.push(broObj) } arr.push(obj) } }; //将抓取的数据以json格式写入本地 fs.writeFile('./pubilc/index.json', JSON.stringify(arr), (err) => { if (err) throw err; console.log('json文件写入成功...') }) //将网页写入本地 fs.writeFile('./pubilc/index.html', str, (err) => { if (err) throw err; console.log('html文件写入成功...') }) });
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。