npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

crawler-lian

v1.0.7

Published

A configuration - based crawler framework

Downloads

1

Readme

description

一个基于配置的爬虫框架,只要配置对应的选择器,就能解析页面的信息,支持配置页面中多个列表的信息、异步处理列表项等功能。

Basic usage

const {fetchBySelector,utils} = require('crawler-lian');
//获取指定元素的属性
fetchBySelector(uri, { selector: 'a', attr: 'href' }).then(({data}) => console.log(data));
fetchBySelector(uri, { selectors:[
    {selector:'.position', attr:'text'},
    {selector:'.phone-num', attr:'text'}
] }).then(({data}) => console.log(data));

//获取一个页面中的指定列表数据和内部数据
fetchBySelector(uri, { groups:[
    {
        groupName: 'list',
        el: '.s_position_list > .item_con_list> .con_list_item',
        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(({data}) => console.log(data));

//其他配置项
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  //处理一组数据
}

api

fetchFile

异步获取文件信息,接收uri和存储路径(默认为files文件夹),返回文件存储位置

fetchFile('www.baidu.com').then(console.log);

craw

crawler的promise支持

craw(uri)
.then(
    ({$}) => console.log($('title').text())
)

fetchByReg

根据正则表达式获取页面匹配的内容数组

fetchByReg('www.baidu.com',/\d+/).then(console.log);

utils

一些工具函数