# spider-crawler
[![Lint & Test](https://github.com/wtto00/spider-crawler/actions/workflows/code-format-test.yml/badge.svg)](https://github.com/wtto00/spider-crawler/actions/workflows/code-format-test.yml) ![coverage](https://img.shields.io/codecov/c/github/wtto00/spider-crawler/main) [![downloads](https://img.shields.io/npm/dm/@wtto00/spider-crawler)](https://www.npmjs.com/package/@wtto00/spider-crawler) ![license](https://img.shields.io/github/license/wtto00/spider-crawler)
定义一个 json 格式的爬虫规则,Nodejs 按照该规则爬取所需要的内容
## 使用
```javascript
import { crawlFromUrl, crawlFromJson, crawlFromHtml } from '@wtto00/spider-crawler';
crawlFromUrl(urlOptions).then((res) => {
console.log(res);
});
crawlFromJson(jsonOptions).then((res) => {
console.log(res);
});
crawlFromHtml(htmlOptions).then((res) => {
console.log(res);
});
```
## 示例
#### crawlFromUrl
```javascript
const options = {
url: 'https://marketplace.visualstudio.com/items?itemName=Orta.vscode-jest',
rules: {
name: {
selector: '.ux-item-name',
handlers: [{ method: 'text' }, { method: 'trim' }],
},
author: {
selector: '.ux-item-publisher',
handlers: [{ method: 'text' }, { method: 'trim' }],
},
installs: {
selector: '.installs-text',
handlers: [
{ method: 'text' },
{ method: 'substring', args: [0, -9] },
{ method: 'trim' },
{ method: 'replace', args: [',', ''] },
{ method: 'number' },
],
},
tags: {
selector: '.meta-data-list-link',
handlers: [
{
method: 'map',
args: [
{
text: { handlers: [{ method: 'text' }] },
link: { handlers: [{ method: 'attr', args: ['href'] }, { method: 'resolveUrl' }] },
},
],
},
],
},
},
};
crawlFromUrl(options).then((res) => {
console.log(res);
});
// {"code":0,"message":"success","data":{"name":"Jest","author":"Orta","installs":1148080,"tags":null}}
```
#### crawlFromJson
```javascript
const options = {
json: JSON.stringify({ test: '1' }),
rules: {
test: {
selector: 'test',
handlers: [{ method: 'number' }],
},
},
};
const res = crawlFromJson(jsonOptions);
// {"code":0,"message":"success","data":{"test":1}}
```
#### crawlFromHtml
```javascript
const options = {
html: '<p class="test">content</p>',
rules: {
content: {
selector: '.test',
handlers: [{ method: 'text' }],
},
},
};
const res = crawlFromHtml(options);
// {"code":0,"message":"success","data":{"content":"content"}}
```
## CrawlFromJson Options
| 字段 | 类型 | 备注 |
| ----- | --------------- | ------------ |
| json | string | json 字符串 |
| rules | [Rules](#Rules) | 取值处理规则 |
## CrawlFromHtml Options
| 字段 | 类型 | 必填 | 备注 |
| ------- | --------------- | ---- | ------------------------------------- |
| baseUrl | string | 否 | baseUrl 用于 html 中某些 url 属性处理 |
| html | string | 是 | html 字符串 |
| rules | [Rules](#Rules) | 是 | 取值处理规则 |
## CrawlFromUrl Options
| 字段 | 类型 | 备注 |
| ------- | --------------------------------------------------------------- | -------- |
| url | string | 请求地址 |
| options | [RequestInit](https://www.npmjs.com/package/node-fetch#options) | 请求参数 |
| rules | [Rules](#Rules) | 爬虫规则 |
### Rules
```typescript
type Rules = Record<string, Rule>;
```
#### Rule
| 字段 | 类型 | 必填 | 备注 |
| -------- | --------------------- | ---- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| selector | string | 否 | [cheerio 选择器](https://github.com/cheeriojs/cheerio/wiki/Chinese-README#%E9%80%89%E6%8B%A9%E5%99%A8) |
| dataType | 'html'\|'json' | 否 | selector 是 [cheerio 选择器](https://github.com/cheeriojs/cheerio/wiki/Chinese-README#%E9%80%89%E6%8B%A9%E5%99%A8),还是 [json 选择器](https://www.lodashjs.com/docs/lodash.at) |
| handlers | [Handler](#Handler)[] | 是 | 爬虫爬取到的元素的处理方法集合 |
#### Handler
```typescript
interface Handler {
method: Method;
args?: Args;
}
```
#### Method & Args
下边列举所有的可以方法以及相对应的参数
| 方法`Method` | 参数`args` | 说明 |
| ------------ | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| prefix | (string) | 字符串开头添加字符串 |
| substring | (number,?number) | 对字符串结果进行截取 |
| replace | (string,string) | 字符串全局替换 |
| trim | - | 去除开头与结尾的空格 |
| number | - | 把字符串转为数字,转换失败时默认为 0
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。
资源推荐
资源详情
资源评论
收起资源包目录
定义一个json格式的爬虫规则,Nodejs按照该规则爬取所需要的内容.zip (35个子文件)
SJT-code
tsconfig.cjs.json 203B
.editorconfig 210B
.vscode
settings.json 504B
launch.json 1KB
.prettierrc 100B
.github
release-drafter.yml 637B
workflows
code-format-test.yml 1KB
npm-package-version-check.yml 1KB
npm-publish-github-packages.yml 2KB
src
crawl.ts 2KB
types.ts 2KB
api.ts 8KB
result.ts 2KB
request.ts 427B
index.ts 87B
util.ts 2KB
LICENSE 1KB
.husky
pre-commit 69B
tsconfig.eslint.json 58B
tests
gitee.com
sum.test.ts 550B
bswtan.com
content.test.ts 1KB
detail.test.ts 4KB
search.test.ts 2KB
error.test.ts 2KB
completion.test.ts 1003B
json.test.ts 2KB
CHANGELOG.md 2KB
package.json 2KB
pnpm-lock.yaml 139KB
.eslintrc.json 641B
jest.config.ts 415B
.gitignore 26B
debugger.ts 2KB
tsconfig.json 881B
README.md 13KB
共 35 条
- 1
资源评论
JJJ69
- 粉丝: 6166
- 资源: 5674
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 四数之和(java代码).docx
- 701837906919458TapScanner v3.0.10 (Pro).apk
- 青岛大学人工智能实验二 利用α-β搜索的博弈树算法编写一字棋游戏
- ### 1、项目介绍 本项目Scrapy进行数据爬取,并使用Django框架+PyEcharts实现可视化大屏 效果如下:
- # 微信小程序-健康菜谱 基于微信小程序的一个查找检索菜谱的应用 ### 效果 !动态图(./res/gif/demo
- zabbix-get命令包资源
- 289ssm-mysql-jsp 计算机课程实验管理系统.zip(可运行源码+数据库文件+文档)
- 毕业设计,基于PyQt5实现的可视化界面的Python车牌自动识别系统源码
- 20-天天果园项目.rar
- 26-朴素贝叶斯分类.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功