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

webtojson

v0.3.9

Published

Use jquery selector to convert web content into json data

Downloads

17

Readme

Use jquery selector to convert web content into json data.

Node.js CI

中文文档

Quick start

  1. Install module
  2. Using modules

Installing the module

npm install webtojson

or

yarn add webtojson

Using modules

Introduce module

var webtojson = require("webtojson");
  1. Implementation method
    Execute the webtojson method

For example, the following code is the result of crawling the google search keyword node;

(async() => {
    var googleData = await webtojson("Https://www.google.com/search?q=node",
      {
        id: ".g",
        title: ".g .r h3"
      },
      {
        headers: {
          "User-Agent": "Mozilla / 5.0(Macintosh; Intel Mac OS X 10_12_6) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 82.0.4083.0 Safari / 537.36",
          "Cookie": "***"
        }
      }
    ).getData();
    console.log(googleData);
})();

result

[{id: 0, title: 'Node.js'},
{id: 1, title: 'Node.js-Wikipedia'},
{id: 2, title: 'nodejs / node: Node.js JavaScript runtime-GitHub'},
{id: 3, title: 'Introduction to Node.js'},
{id: 4, title: 'Node.js Introduction-W3Schools'},
{id: 5, title: 'Node-Web APIs | MDN-Mozilla'},
{id: 6, title: 'node-npm'},
{id: 7, title: 'node-Docker Hub'},
{id: 8, title: 'Express-Node.js web application framework'},
{id: 9, title: 'Node.js-Introduction-Tutorialspoint'},
{id: 10, title: 'Node-RED'}]

API

The module provides the following methods:

  • webtojson(required)
  • extend(optional)
  • add(optional)
  • getData or saveFile(required)

webtojson

webtojson(urls, selector, option); webtojson method can grab single page data;

  • urls: string | array | collection The url address of the content to be crawled. It can be a single url or multiple;(required)
  • selector: object specifies the content to be crawled;(required)
  • option: object configuration item(optional)
    • paging: paging configuration(optional)
      • pageNum: total pages
      • offset: number offset of each page
      • keyword: keyword corresponding to string url
    • headers: request header settings(optional)
      • User-Agent: Specify useragent(optional)
      • Cookie: Make a cookie. For example, some search engines need to provide cookies to have results (optional)

Urls supports three types of strings, arrays, and collections:

When urls is a string, only the content of a single url is crawled. as follows:

webtosjon ("https://www.google.com/search?q=node", selector, option);

When urls is an array, it will grab all the url addresses in the array and merge all the values together. as follows:

webtosjon (["https://www.google.com/search?q=node", "https://www.google.com/search?q=javascript"], selector, option);

When urls is the collection, object url field will be used and all values are merged together. as follows:

webtosjon ([
     {
       title: "search node",
       url: "https://www.google.com/search?q=node",
     }, {
       title: "search javasript",
       url: "https://www.google.com/search?q=javascript",
     }
], selector, option);

The following is a form of selector:

{
    id: ".typecont span a",
    title: ".typecont span a",
    url: ".typecont span a"
}

selector is of the form key / value, key is the attribute name of the final json, and value is the attribute value of json;

value has two types: String and Function;

When value is String, it is a selector similar to jquery, and the content of the selector corresponding to the dom node is used as the json attribute value.

When value is Function, the value that the function runs as the json attribute value; something like this:

{
    id: ".typecont",
    title: ".typecont span a",
    url: function ($, parentEle) {
        return $(parentEle) .find("a"). attr("href");
    }
}

The function has two parameters:

  • $: jquery $
  • parentEle: parent element node, select child nodes based on parent element;

extend(selector) or extend(url, selector)

Data from other pages will be captured and merged with previous data;

There are two different parameters for extend:

extend(selector): if there is only a selector parameter, the value of the previous data url field will be used to ;

extend(url, selector): same as webtojson function parameters;

var googleData = await webtojson(
"Https://www.google.com/search?q=node",
      {
        id: ".g",
        title: ".g .r h3",
        url: ".g .r a"
      }).extend({
        content: "title"
      }).getData();

extend merges the search list name with the contents of each name;

add(selector) or add(url, selector)

Add information on the basis of previous data, parameters are consistent with extend method;

var googleData = await webtojson(
"Https://www.google.com/search?q=node",
      {
        id: ".g",
        title: ".g .r h3",
        url: ".g .r a"
      }).add({
        content: "title"
      }).getData();

getData()

Get data for the entire chain;

saveFile(filePath)

Save the data as a file

var googleData = await webtojson("https://www.google.com/search?q=node", {
    id: ".g",
    title: ".g .r h3",
    url: ""
}).saveFile("./google.json");