-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f3ed0cb
Showing
4 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Ignore output of scraper | ||
data.sqlite |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a scraper that runs on [Morph](https://morph.io). To get started [see the documentation](https://morph.io/documentation) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"dependencies": { | ||
"cheerio": "latest", | ||
"request": "latest", | ||
"sqlite3": "latest" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// This is a template for a Node.js scraper on morph.io (https://morph.io) | ||
|
||
var cheerio = require("cheerio"); | ||
var request = require("request"); | ||
var sqlite3 = require("sqlite3").verbose(); | ||
|
||
function initDatabase(callback) { | ||
// Set up sqlite database. | ||
var db = new sqlite3.Database("data.sqlite"); | ||
db.serialize(function() { | ||
db.run("CREATE TABLE IF NOT EXISTS data (name TEXT)"); | ||
callback(db); | ||
}); | ||
} | ||
|
||
function updateRow(db, value) { | ||
// Insert some data. | ||
var statement = db.prepare("INSERT INTO data VALUES (?)"); | ||
statement.run(value); | ||
statement.finalize(); | ||
} | ||
|
||
function readRows(db) { | ||
// Read some data. | ||
db.each("SELECT rowid AS id, name FROM data", function(err, row) { | ||
console.log(row.id + ": " + row.name); | ||
}); | ||
} | ||
|
||
function fetchPage(url, callback) { | ||
// Use request to read in pages. | ||
request(url, function (error, response, body) { | ||
if (error) { | ||
console.log("Error requesting page: " + error); | ||
return; | ||
} | ||
|
||
callback(body); | ||
}); | ||
} | ||
|
||
function run(db) { | ||
// Use request to read in pages. | ||
fetchPage("https://morph.io", function (body) { | ||
// Use cheerio to find things in the page with css selectors. | ||
var $ = cheerio.load(body); | ||
|
||
var elements = $("div.media-body span.p-name").each(function () { | ||
var value = $(this).text().trim(); | ||
updateRow(db, value); | ||
}); | ||
|
||
readRows(db); | ||
|
||
db.close(); | ||
}); | ||
} | ||
|
||
initDatabase(run); |