improve usability for multiple websites crawling

This commit is contained in:
spiduler
2020-10-12 03:30:57 +09:00
parent e3c2dd248f
commit 25dcb68e85

View File

@@ -7,7 +7,6 @@ const logger = require("logops")
class SeleniumBrowser { class SeleniumBrowser {
constructor() { constructor() {
this.website = 'https://www.real.de/'
this.driver = null this.driver = null
this.config = { useHub: true, browser: 'chrome' } this.config = { useHub: true, browser: 'chrome' }
this.capabilities = Capabilities.chrome() this.capabilities = Capabilities.chrome()
@@ -18,9 +17,6 @@ class SeleniumBrowser {
async setDriver() { async setDriver() {
if (this.config.useHub) { if (this.config.useHub) {
let opts = new Options; let opts = new Options;
// opts.setAcceptInsecureCerts(true);
// opts.setBrowserVersion('67');
// opts.setPlatform('Windows XP');
opts.addArguments(this.getChromeArgs()) opts.addArguments(this.getChromeArgs())
this.driver = await new Builder() this.driver = await new Builder()
.usingServer('http://spiduler-chrome:4444/wd/hub') .usingServer('http://spiduler-chrome:4444/wd/hub')
@@ -36,29 +32,25 @@ class SeleniumBrowser {
setLocalDriver() { setLocalDriver() {
let opts = new Options let opts = new Options
opts.addArguments(this.getChromeArgs()) opts.addArguments(this.getChromeArgs())
// opts.setChromeBinaryPath('/usr/bin/chromedriver')
return new Builder().forBrowser(this.config.browser).setChromeOptions(opts).build(); return new Builder().forBrowser(this.config.browser).setChromeOptions(opts).build();
// let driver = new
// webdriver.Builder().forBrowser('chrome').setChromeOptions(this.getChromeArgs()).build();
} }
async request(uri) { async request(uri) {
if (this.isRecaptchaReseponseEmpty) { if (this.isRecaptchaReseponseEmpty) {
return '' return ''
} else { } else {
logger.info('Request URL %s', this.website + uri) logger.info('URL %s', uri)
return this.driver.get(this.website + uri).then(d => this.driver.findElement(By.css('body')).getAttribute('innerHTML')) if (uri.indexOf('://') > 3) {
return this.driver.get(uri).then(d => this.driver.findElement(By.css('body')).getAttribute('innerHTML'))
.then(html => this.getHtml(html)) .then(html => this.getHtml(html))
// return await }else {
// driver.findElement(By.css('body')).getAttribute('innerHTML') logger.info('URL %s is not valid', uri)
// .sendKeys('webdriver', Key.RETURN); return ''
/// await driver.wait(until.titleIs('webdriver - www.real.de'), 1000); }
} }
} }
async getHtml(html) { async getHtml(html) {
// return html
// check if it's forbbidden page
if (html.indexOf('grecaptcha') > -1 || html.indexOf('www.google.com/recaptcha/api2/anchor') > -1) { if (html.indexOf('grecaptcha') > -1 || html.indexOf('www.google.com/recaptcha/api2/anchor') > -1) {
this.isRecaptchaReseponseEmpty = true this.isRecaptchaReseponseEmpty = true
let iframeSrc = await this.driver.findElement(By.css('#grecaptcha iframe')).getAttribute('src') let iframeSrc = await this.driver.findElement(By.css('#grecaptcha iframe')).getAttribute('src')
@@ -83,7 +75,7 @@ class SeleniumBrowser {
do { do {
maxRetry = maxRetry - 1 maxRetry = maxRetry - 1
res = await this.requestCaptchaResult(res, requestId) res = await this.requestCaptchaResult(res, requestId)
if(maxRetry == 0) break if (maxRetry == 0) break
} while (res.request == 'CAPCHA_NOT_READY') } while (res.request == 'CAPCHA_NOT_READY')
this.isRecaptchaReseponseEmpty = false this.isRecaptchaReseponseEmpty = false
return res return res