Giter Site home page Giter Site logo

node-image-scraper's Introduction

Node Image Scraper

Scrape images from the web easily. Usage

The image-scrape module provides a class which needs to be constructed with a url:

var Scraper = require("image-scraper");

var scraper = new Scraper("https://apod.nasa.gov/apod/astropix.html");

The url can be changed easily:

scraper.address = "http://www.npmjs.org";

The scraper object provides the .scrape() method. The scrape method accepts one optional argument: a callback function.

This callback function can also be set using the .on() method.

scraper.on("image", function(image){

	// Do something.	
});

As soon as 'scraper.scrape()' is called, the callback will be fired with every image found on the webpage, note however that dynamically generated images will not be found.

The image object that is passed to the callback has the following properties and methods:

// The attributes found in the image tag, which is parsed by Cheerio (https://npmjs.org/package/cheerio).
image.attributes;
// The basename of the image.
image.name;
// Absolute path to the folder the image will be saved to.
image.saveTo;
// Extension of the image file.
image.extension;
// The absolute URL of the image.
image.address;
// The URL of the page the image is scraped from.
image.fromAddress;
// Save the image.
image.save();

The behaviour of image.save() can be changed by setting the name, saveTo, and extension properties. The saveTo property is by default set to the current directory, and the other two properties are by default set to the data found in the src attribute of the img tag.

Thus, the smallest program that scans a webpage for images and saves them in the current directory looks as follows:

var Scraper = require('image-scraper');
var scraper = new Scraper('https://apod.nasa.gov/apod/astropix.html');

scraper.scrape(function(image) { 
	image.save();
});

node-image-scraper's People

Contributors

dbergan avatar leon-vv avatar rv-kip avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.