se-scraper/index.js

75 lines
2.2 KiB
JavaScript
Raw Normal View History

2018-12-24 14:25:02 +01:00
const handler = require('./src/node_scraper.js');
var fs = require('fs');
var os = require("os");
2018-12-24 14:25:02 +01:00
2019-01-26 20:15:19 +01:00
exports.scrape = async function(config, callback) {
2018-12-24 14:25:02 +01:00
// options for scraping
event = {
// the user agent to scrape with
user_agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36',
// if random_user_agent is set to True, a random user agent is chosen
2019-01-30 23:53:09 +01:00
random_user_agent: true,
2018-12-24 14:25:02 +01:00
// whether to select manual settings in visible mode
2019-01-26 20:15:19 +01:00
set_manual_settings: false,
2018-12-24 14:25:02 +01:00
// get meta data of scraping in return object
2019-01-30 23:53:09 +01:00
write_meta_data: false,
2019-01-26 20:15:19 +01:00
log_http_headers: false,
2018-12-24 14:25:02 +01:00
// how long to sleep between requests. a random sleep interval within the range [a,b]
// is drawn before every request. empty string for no sleeping.
sleep_range: '[1,1]',
// which search engine to scrape
search_engine: 'google',
2019-01-26 20:15:19 +01:00
compress: false, // compress
debug: false,
verbose: false,
keywords: ['scrapeulous.com'],
2019-01-26 20:15:19 +01:00
// whether to start the browser in headless mode
headless: true,
// path to output file, data will be stored in JSON
output_file: '',
// whether to prevent images, css, fonts and media from being loaded
// will speed up scraping a great deal
2019-01-27 15:54:56 +01:00
block_assets: true,
// path to js module that extends functionality
// this module should export the functions:
// get_browser, handle_metadata, close_browser
//custom_func: resolve('examples/pluggable.js'),
2019-01-27 20:08:09 +01:00
custom_func: '',
// use a proxy for all connections
// example: 'socks5://78.94.172.42:1080'
// example: 'http://118.174.233.10:48400'
proxy: '',
2018-12-24 14:25:02 +01:00
};
// overwrite default config
2018-12-24 14:25:02 +01:00
for (var key in config) {
event[key] = config[key];
}
if (fs.existsSync(event.keyword_file)) {
2018-12-24 14:25:02 +01:00
event.keywords = read_keywords_from_file(event.keyword_file);
}
if (!callback) {
// called when results are ready
callback = function (err, response) {
if (err) {
console.error(err)
}
console.dir(response.results, {depth: null, colors: true});
}
}
2019-01-26 20:15:19 +01:00
await handler.handler(event, undefined, callback );
2018-12-24 14:25:02 +01:00
};
function read_keywords_from_file(fname) {
let kws = fs.readFileSync(fname).toString().split(os.EOL);
2018-12-24 14:25:02 +01:00
// clean keywords
kws = kws.filter((kw) => {
return kw.trim().length > 0;
});
return kws;
}