Socket
Socket
Sign inDemoInstall

crawler-lian

Package Overview
Dependencies
0
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    crawler-lian

```javascript const {fetchBySelector,utils} = require('crawler-lian'); //获取所有链接 fetchBySelector(uri, { selector: 'a', attr: 'href' }).then(console.log);


Version published
Maintainers
1
Install size
39.6 kB
Created

Readme

Source

Basic usage

const {fetchBySelector,utils} = require('crawler-lian');
//获取所有链接
fetchBySelector(uri, { selector: 'a', attr: 'href' }).then(console.log);

//获取一个页面中的指定列表数据和内部数据
fetchBySelector(uri, { groups:[
    {
        groupName: 'list',
        el: '.s_position_list > .item_con_list> .con_list_item',
        ...require('./list'),
        selectors:[
            {
                selector: '.position_link',
                attr: 'href',
                name: 'detail_url'
            },
            {
                selector: '.format-time',
                attr: 'text',
                name: 'time',
                handler({ value }) {
                    return parseTime(value);
                }
            },
        ],
        //处理具体值
        handler({ value }) {
            return utils.removeSpace(value);
        },
        //合并/处理 数据项
        process ({ matchs }) => {
            if (matchs && matchs.length > 0) {
                return matchs[0]
            }
        },
        //获取列表项内部页面数据,并合并到当前项
        itemProcess({ data }) {
            let detail_url = data.detail_url;
            if (detail_url) {
                return new Promise((resolve) => {
                    fetchBySelector(detail_url, detailOptions).then(({ data: detailData }) => {
                        resolve({ ...data, ...detailData });
                    }).catch(console.log)
                })
            } else {
                // console.log(data)
                return data;
            }
        }
    }
] }).then(console.log);

//其他配置项
const option = {
    deDuplication: false,  //是否去重
    selector: 'a', //默认选择器
    attr: 'text', //默认选择的属性
    trim: true, //是否去前后空格
    handler: null,  //处理器,处理选中的具体元素
    process: null,  //处理选择器选中的一个数组,返回新的数据或者一个promise对象
    test: null, //测试标准,传入一个正则表达式
    filter:null, //过滤器,与test功能相同,传入一个函数
    groups, //分组爬取, 如果和selector同时存在,会覆盖selector
    itemProcess: null  //处理一组数据
}

Keywords

FAQs

Last updated on 06 Apr 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc