Doggo Scraping
Doggo is a little friend to help you make scraping scripts a lot easier.
How it Works?
The main approach under Doggo Scraping is to abstract semantically(and programmatically) things you don't need to worry about by providing a super-api(ironic content alert) of methods made of: a starter(wakeUp) with a built-in ender, an iterator(iterate), a redirector(goto) and a DOM evaluable(eval).
Dependencies
This project runs over a headless-chrome instance (you gonna need 59+ chrome version). To initialize it and make use of some DevTools Protocol API utilities, we're using in this project lighthouse's chrome-launcher. But, for the DOM's handling, we choose to work with cheerio, which implements jQuery's core to make powerfull evaluations.
Instalation
$ npm install doggo-scraping --save
API
wakeUp(url, callback(doggoInstance)).then(function(){})
Method to initialyze the scraping block. Internally lauches headless-chrome
, calls goto(url)
method, execute a callback(where things happens) and after that, kill chrome instance ordering doggo to die()
(but he's just pretending...no, he's not!).
... doggo;
...or even:
... doggo ;
goto(url).then(function(){})
Method to open a new page. Once a page is ready, it resolves a new instance of doggo with its methods (as you just saw above) according to the new DOM's context. Furthermore, persists the url param.
... return doggoAtHome ;...
...or maybe a chain of them:
... return doggoAtHome ;...
eval('selector')
Evaluates a selector (according to instance context) and returns a "jquery element".
... let goodBoy = doggoAtHome; console...
iterate('selector', callback(item, index)).then(function(){})
Our iterator method (also a Promise), receives a selector to iterate over it, returning on each iteration, item by item as "jQuery element" (internally calls eval('selector')
).
... return doggo;...
OBS: As you may noticed in the isolated pieces of code above and will see at the Hands On section, the promises goto()
and iterate()
always return itselves. That's because we need promises respect a "hierarchical" behaviour (resolve to continue).
Hands On
In the fallowing script, we're going to get all books from the site 'http://editoraunicamp.com.br/' and set them to their respective categories. To get there, we will:
goto()
the page where the categories are;iterate()
over the category items;goto()
these categories pages to see our books;iterate()
over all books elements;goto()
each book's details page;- handle the elements(
eval()
) and keep these books and categories until the last one; - finally, we show up(
console.log()
) the results kepts;
// Since Doggo Scraping is a class, we need to require it and instantiate a new object const DoggoScraping = ; const doggo = ; // simple counter to totalize books at the end let count = 0; /* * Next, we're using our object to initialize a scraping script block. * At this moment, the object doggo cannot do much. We're gonna initialize it * to have access to the other methods exposed at doggoAtHome */ doggo ;
That's all folks!
This package isn't mature enough and has a long way with many possibilities to go. If you have any suggestion or found a bug, let me know!
License
MIT License
Copyright (c) 2017 Rodrigo Brabo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.