@baige/request
v2.2.4
Published
一个专注于爬虫的网络请求器,后可能会改名为@baige/cjc
Downloads
89
Readme
@baige/request
介绍
一个专注于爬虫的网络请求器,后可能会改名为@baige/cjc
软件架构
百分之 90 使用 node 原生的 http 和 https 模块开发,少部分功能调用了第三方库,如请求的数据如果被压缩的话,解压就是调用的第三方库实现的。 还有代理功能也是通过第三方库实现的
安装教程
npm i @baige/request
使用说明
主体模块的各个参数和方法(Cjc 类)
| 名称 | 入参 | 功能 | | ---------------------- | -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------- | | get | url: string, data: RequestOption|object ,option:RequestOption | get 请求,url:请求路径,data:可以是请求参数也可以是附加参数对象,option:附加参数对象 | | | | 注:data 对象中如果有$option:true,则表名 data 是一个附加参数对象(option),如果 data 为 option 的话,真正的请求参数也可写在 option.data 中 | | post | url: string, data: RequestOption|object ,option:RequestOption | post,put,delete 请求 content-type 默认为 json,其他同 get | | put | url: string, data: RequestOption|object ,option:RequestOption | 同 get | | delete | url: string, data: RequestOption|object ,option:RequestOption | 同 get | | upload | url: string, data: RequestOption|object ,option:RequestOption | 文件上传方法,data 中的值需要需要对应,{[任意 key]:文件绝对路径} | | globalHeaders | 变量可直接赋值 | 全局请求头 | | globalTimeout | 变量可直接赋值 | 全局默认超时时间,单位:秒 | | globalCookie | 变量可直接赋值 | 全局 Cookie 对象 | | globalHook | 变量可直接赋值 | 全局钩子{beforeRequest:请求前调用,afterRequest:请求后调用} | | setGlobalFrequency | s:number | 设置全局频率,单位:秒 | | setLocalRegexFrequency | regex: RegExp, s: number | 设置正则匹配频率:regex:正则,s:频率秒 | | setPathFrequency | path: string, s: number | 设置路径匹配频率:path:路径,s:频率秒 | | request | option:RequestOption | 请求方法,get、post 等方法最终调用的是这个方法 |
option(RequestOption 类)参数讲解
| 名称 | 功能 | 类型 | 默认值 | | --------------------- | --------------------------------------------------------------------------------------------------------- | -------------------------------------------- | --------- | | url | 请求路径 | string | 空字符 | | method | 请求方式(get、post、put、delete) | string | undefined | | headers | 请求头 | object | 空对象 | | data | 请求参数 | object | 空对象 | | cookieTree | cookieTree 树,cookie 的树结构,爬虫利器 | object | 空对象 | | proxy | 代理 ,配置后请求将走代理路线 | {host:string,port:number} | undefined | | frequency | 请求频率,单位秒,请求的间隔时间, 0 为无限制,将不受全局频率等影响 | number | undefined | | regexFrequency | 正则频率,如果为 true 则进行正则匹配路径查找频率 | boolean | false | | cookie | cookie 对象,内部类型,大多数不需要管这个参数 | class | Cookie | | timeout | 超时时间(单位:秒) | number | undefined | | parse | 是否允许解析返回的数据,true:将解析为字符串并根据 content-type 反序列化为对象,false:将只有 buffer 数据 | boolean | true | | forceDeserialization | 是否强制反序列化,不考虑 content-type 类型 | boolean | false | | repeatDeserialization | 是否重复反序列化,有的接口可能需要多次反序列化才能变为 object | boolean | true | | download | 是否下载 | boolean | false | | saveFilePath | 下载文件的保存路径,可为空将自动生成路径,下载完成将返回文件路径(可选) | string | undefined | | saveFileDir | 下载文件的文件夹(可选) | string | undefined | | saveFileName | 下载文件的名称(可选) | string | undefined | | | 注:saveFilePath 的优先级大于 saveFileDir 和 saveFileName | | | | original | 是否不做任何操纵,返回原始的 res | boolean | false | | logs | 是否开启日志,开启日志后建调用@baige/logc 的日志工具,打印请求详情 | boolean | false | | dataMatching | 数据匹配,返回的数据将是设置的 key:正则匹配的数据 number 为下标-1 为全部 | { [key: string]: RegExp | [RegExp,number] } | {} | | check | 数据校验,校验失败将不执行 dataMatching | RegExp | undefined | | redirect | 是否允许自动重定向 | Boolean | true | | hook | 请求钩子,beforeRequest(option)请求前,afterRequest(res)请求后 | {beforeRequest,afterRequest} | {} | | hasRequest | 是否需要返回请求体 | boolean | false | | hasResponse | 是否返回响应体 | boolean | false | | hasBuffer | 是否返回响应数据的 buffer | boolean | false | | retry | 请求异常的话重试的次数 | number | 3 |
返回类型(Response 类)内容讲解
| 名称 | 功能 | 类型 | 默认值 | | ---------- | ------------------------------------------------------------------------------ | -------------------- | --------- | | res | 响应体,原始的响应类 | http.IncomingMessage | undefined | | statusCode | 响应状态码(200、404...) | number | undefined | | headers | 响应头,内部可能会有 set-cookie 等字段,当然 cookie 都会被封装到 cookieTree 中 | object | undefined | | cookie | 截止到此次请求的所有 cookie 信息 | string | undefined | | cookieTree | 截止到此次请求的所有 cookie 信息的树形式,可直接构建成 Cookie 对象 | object | undefined | | data | 返回的经过解析后的数据 | any | undefined | | buffer | 返回的二进制数据 | buffer | undefined | | check | 校验结果 | boolean | true |
Cookie 类
| 名称 | 入参 | 功能 | | --------------- | --------------------------------------- | ------------------------------------------------------------------------------------------------------------- | | encapsulation | url:string | 根据 url 获取这个 url 对应的所有 cookie,注意是获取 url 对应的 cookie,而不是获取 url 的域名对应的所有 cookie | | add | url:string,data:string|IncomingMessage | 向 Cookie 中添加 cookie | | delete | url:string | 删除某个网址下指定 path 的所有 cookie | | empty | url:string | 清空 url 路径下的 cookie,从根开始清除 | | mergeCookieTree | cookie: object | 合并 cookieTree | | getCookieTree | | 获取 cookie 树 |
参与贡献
完全独立个人开发
特技
可根据不同的参数设置爬虫所需要的不同功能