赞
踩
示例网站已添加
反爬处理,superagent
部分仅获取到包含js
跳转功能的部分html
,非完整页面html
;
针对未添加反爬处理的网站,本文逻辑依旧有效
URL: https://s.weibo.com/top/summary?cate=realtimehot ;
mkdir antd-course
cd antd-course
npm init -y // -y命令代表yes, 省略默认选项点击
npm i cheerio superagent -D
一个轻量级、渐进式的请求库,内部依赖 nodejs 原生的请求 api,适用于 nodejs 环境;
nodejs 的抓取页面模块,为服务器特别定制的,快速、灵活、实施的 jQuery 核心实现;
适合各种 Web 爬虫程序。node.js 版的 jQuery ;
创建test.js文件
const cheerio = require("cheerio");
const superagent = require("superagent");
const fs = require("fs");
const weiboURL = "https://s.weibo.com"; // 域名
const hotSearchURL = weiboURL + "/top/summary?cate=realtimehot"; // 路径
(1)参数:2 个;(请求的 url ,请求成功后的回调函数);
(2)回调函数参数:2个;(error 【成功,则返回 null,反之则抛出错误】, 成功后的 响应体);
# hotSearchURL :请求的Url;
# err : 回调函数第一参数[ 成功,则返回 null,反之则抛出错误 ];
# res : 请求的响应体;
superagent.get(hotSearchURL, (err, res) => {
if (err) console.error(err);
});
如图:
【作用】:达成 nodejs 中,可以写 jQuery 语法的效果;
// 包装请求后的响应体 ;
const $ = cheerio.load(res.text);
let hotList = [];
$("#pl_top_realtimehot table tbody tr").each(function (index) {
// 拼接数组元素;
if (index !== 0) {
const $td = $(this).children().eq(1);
const link = weiboURL + $td.find("a").attr("href");
const text = $td.find("a").text();
const hotValue = $td.find("span").text();
const icon = $td.find("img").attr("src")
? "https:" + $td.find("img").attr("src")
: "";
// 元素push进数组;
hotList.push({
index,
link,
text,
hotValue,
icon,
});
}
});
fs.writeFileSync(file, data[, options]).
// node.js文件模块fs;
fs.writeFileSync(
'qq.json',
JSON.stringify(hotList),
"utf-8"
);
node test.js
结果:
打开生成的qq.json文件可见, 爬取成功;
npm i node-schedule
* * * * * *
┬ ┬ ┬ ┬ ┬ ┬
│ │ │ │ │ │
│ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun)
│ │ │ │ └───── month (1 - 12)
│ │ │ └────────── day of month (1 - 31)
│ │ └─────────────── hour (0 - 23)
│ └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
// 分割线: 上方为对应使用规则 ;
const nodeSchedule = require("node-schedule");
const rule = "30 * * * * *"; // 30秒刷新一次,输出时间点;
nodeSchedule .scheduleJob(rule, () => {
console.log(new Date());
});
const cheerio = require("cheerio");
const superagent = require("superagent");
const fs = require("fs");
const nodeSchedule = require("node-schedule");
const weiboURL = "https://s.weibo.com";
const hotSearchURL = weiboURL + "/top/summary?cate=realtimehot";
// 封装单词抓取数据函数为async 函数,返回Promise对象,;
function getHotSearchList() {
return new Promise((resolve, reject) => {
superagent.get(hotSearchURL, (err, res) => {
if (err) reject("request error");
const $ = cheerio.load(res.text);
let hotList = [];
$("#pl_top_realtimehot table tbody tr").each(function (index) {
if (index !== 0) {
const $td = $(this).children().eq(1);
const link = weiboURL + $td.find("a").attr("href");
const text = $td.find("a").text();
const hotValue = $td.find("span").text();
const icon = $td.find("img").attr("src")
? "https:" + $td.find("img").attr("src")
: "";
hotList.push({
index,
link,
text,
hotValue,
icon,
});
}
});
hotList.length ? resolve(hotList) : reject("errer");
});
});
}
// 利用node包“ nodeSchedule” 每隔30秒,执行async函数;
nodeSchedule.scheduleJob("30 * * * * *", async function () {
// 捕捉错误
try {
const hotList = await getHotSearchList(); // 阻塞代码,直到拿到resolve的值,此处即 hotList;
await fs.writeFileSync(
`app.json`,
JSON.stringify(hotList),
"utf-8"
);
} catch (error) {
console.error(error);
}
});
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。