# spider-website

> spider website

Latest version **1.0.7** (published 2018-08-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install spider-website
pnpm add spider-website
yarn add spider-website
bun add spider-website
```

Provides the command `spider`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2018-08-22 |
| First published | 2017-01-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 12 |
| Unpacked size | 398.9 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 18 |
| Author | laomu1988@qq.com |
| Maintainers | laomu |

## Links

- npm: https://www.npmjs.com/package/spider-website
- Repository: https://github.com/laomu1988/spider-website
- Homepage: https://github.com/laomu1988/spider-website#readme
- Issues: https://github.com/laomu1988/spider-website/issues
- npm.io page: https://npm.io/package/spider-website

## Dependencies (12)

- [death](https://npm.io/package/death.md) ^1.1.0
- [debug](https://npm.io/package/debug.md) ^2.6.0
- [yargs](https://npm.io/package/yargs.md) ^6.6.0
- [is-dir](https://npm.io/package/is-dir.md) ^1.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.13.1
- [mk-dir](https://npm.io/package/mk-dir.md) ^1.0.1
- [cheerio](https://npm.io/package/cheerio.md) ^0.20.0
- [request](https://npm.io/package/request.md) ^2.72.0
- [ctx-links](https://npm.io/package/ctx-links.md) ^1.0.2
- [temp-data](https://npm.io/package/temp-data.md) ^1.0.3
- [iconv-lite](https://npm.io/package/iconv-lite.md) ^0.4.15
- [convert-relative](https://npm.io/package/convert-relative.md) ^1.0.2

## Recent versions

- 1.0.7 (latest) — 2018-08-22
- 1.0.6 — 2018-08-22
- 1.0.5 — 2017-03-13
- 1.0.4 — 2017-03-06
- 1.0.3 — 2017-02-13
- 1.0.2 — 2017-02-10
- 1.0.1 — 2017-01-19

## README

# 网站爬虫（自动下载工具）

## 安装
安装node之后执行
```
npm install spider-website -g
```


## 命令行
```
spider weburl [--save folder]
```
下载 weburl 网站及相关链接到 folder目录下， 默认保存到spider目录

## js使用说明
```
var Spider = require('spider-website');

// 下载起始网址
var index = 'http://laomu1988.github.io/index.html';

var spider = new Spider({url: index, saveTo: __dirname + '/save/'});


spider.on('loaded', function(file) {
    console.log('下载文件:',file.link);
});

spider.load();

```

## config
* url: 下载起始链接
* saveTo: 保存文件目录

## api
* load()      : 开始下载
* stop()      : 停止下载
* clean()     : 清空下载历史数据
* update(link): 更新数据
* remove(link): 移除下载链接
* has(link)   : 下载链接是否加入列表
* get(link)   : 取得下载对象

## event
* push_before 添加链接前触发，参数(file),将file.link置为空或者false将取消添加链接
* push      文件加入下载列表时触发,参数(file)
* load_before 下载文件前触发
* loaded    下载成功触发, 参数(file,body,response)
* load_fail 下载失败触发, 参数(file, response || err)
* before_save 保存到本地前触发
* load_finish 列表全部下载完毕后触发

## spider
* spider.config: 配置内容
* spider.links: {link: file} 所有文件及其状态，改对象的key是文件的下载地址
* spider.list: [link,link],所有下载的或者未下载的文件列表

## File对象
* construnct(href|data, opts)
* load() 下载数据
* isHTML() 是否是html文件
* isText() 是否是文本文件
* getLinks() 获取文件中的链接
* getBody() 获取body主体
* 属性
    - link: 下载地址，不包含query和hash部分
    - href: 全部地址，包含query和hash
    - protocol: http:或者https:
    - host: 网址host
    - pathname： 网址路径
    - saveTo: 文件保存地址，不包括config.saveTo的部分
    - loadState：  文件下载状态，0：未下载，1：下载中，2：下载成功， 3：下载失败
    - reTryTime: 重试了多少次
    - hash:      文件内容的hash值,可用来判断文件是否改变


## spider处理流程
1. 计算页面保存位置等属性
1. 将文件加入下载列表
1. 从下载列表中取出一个要下载的文件
1. 下载文件到保存位置
1. 下载文件引用的地址，判断引用地址是否需要下载，假如需要下载则加入到下载列表
1. 修改下载文件引用地址为相对地址（避免引用位置错乱）


## todo
* [x] 使用es6 class改写spider
* [x] 命令行下载数据: spider [website]
* [x] 文件中绝对路径改为相对路径
* [ ] 根据服务器返回编码，自动转换为utf8格式
* [ ] 取消配置文件编码,当时gbk时下载文件完毕后自动转换为utf8格式
* [ ] 下载地址为网站的某一个子目录下文件则默认配置为只下载该子目录下的文件，其他文件仍然指向原网址
* [ ] 中断下载后继续下载
* [ ] 本地数据库存储
    - [x] 本次配置,配置是否变更
    - [x] 要下载的文件列表
    - [x] 文件状态更新
    - [ ] 是否存在未下载的文件
* [x] has 是否存在某个链接
* [x] remove 移除某个链接
* [ ] 文件链接解析
    - [x] html文件引入其他: html,js,css,img
    - [ ] css文件引入img
* [ ] 判断文件变更: hash或者query变化
    - [ ] query变更
    - [ ] 文件内容变更
* [ ] api
    - [ ] 取得所有文件
    - [ ] 输出当前进度
* [ ] 事件
    - [ ] 下载完毕事件
    - [ ] 解析完毕事件

## version
* v1.0.7
    - fixbug: spider命令行修复
* v1.0.6
    - fixbug: death依赖未加入package.json

---
_Source: https://npm.io/package/spider-website · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
