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

spiderrequest

v1.0.7

Published

spdier request

Downloads

13

Readme

spiderRequest

通过简单的设置配置文件,获取扒取网页信息,返回数据。

起步

下面看一个简单的例子,只需设置几个简单的属性,就可以得到想要的到的数据。

//引用spiderRequest
const spiderRequest = require("spiderrequest");

//定义一个要扒取的网页网页信息,以百度首页为例,获取‘百度一下’按钮的值
let config = {
	url: 'https://www.baidu.com/',
	encode: 'utf8',
	content: {
		'name':'btn',
		'selector': '#su',
		'value':['attr','value']
	}
};
new spiderRequest(config).getContent()
    .then((data)=>{
    	console.log(data);
    });
//输出{ btn: '百度一下', location: 'https://www.baidu.com/' }

配置

  1. name:扒取数据最终以json的形式返回,那么是value获取值对应的key。
  2. selector:获取对应的dom节点,遵守cheerio的语法,与jquery获取dom节点类似。需要注意的是,其实在上级dom节点的基础上找子节点。
  3. eq:如果通过selector获取的dom节点有多个,通过设置eq的值确定获取那个dom节点。
  4. value:数组形式,如果selector获取的节点有要获取的值,则通过以下配置直接获取值。
    • ['text']:对节点执行text()
    • ['html']:对节点执行html()
    • ['attr','value']:对节点执行attr('value')
    • ['data','name']:对节点执行data('name') 也可以传入对象数组,用于查找其子节点的值。子节点的参数定义与options一致。
  5. machining:定义一个函数,用于对该节点取值进行加工,例如两端去除空格,截取字符串等操作。函数接受两个参数,分别是当前节点的值和当前页面路径。需要注意的是,函数必须有返回值,并且该节点必须为值,而不能为子节点。
'machining':(value,location)=>{
	return 'test:'+value; 
}

接口

getContent:才用es6的promise语法,通过new spiderRequest(config)初始化spiderRequest对象,并通过调用getContent方法获取爬取的 数据,该方法返回一个promise对象。