-
Notifications
You must be signed in to change notification settings - Fork 24
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
1 parent
e85312d
commit 2ee0be7
Showing
6 changed files
with
87 additions
and
10 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
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
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
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,26 @@ | ||
module Dhalang | ||
# Provides functionality for scraping webpages. | ||
class Scraper | ||
SCRIPT_PATH = File.expand_path('../js/html-scraper.js', __FILE__).freeze | ||
private_constant :SCRIPT_PATH | ||
|
||
# Scrapes full HTML content under given url. | ||
# | ||
# @param [String] url Url to scrape. | ||
# @param [Hash] options User configurable options. | ||
# | ||
# @return [String] Scraped HTML content. | ||
def self.html(url, options = {}) | ||
UrlUtils.validate(url) | ||
temp_file = FileUtils.create_temp_file("html") | ||
begin | ||
configuration = Configuration.new(options, url, temp_file.path, "html") | ||
NodeScriptInvoker.execute_script(SCRIPT_PATH, configuration) | ||
html = IO.read(temp_file.path) | ||
ensure | ||
FileUtils.delete(temp_file) | ||
end | ||
return html | ||
end | ||
end | ||
end |
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,26 @@ | ||
'use strict'; | ||
const dhalang = require('./dhalang'); | ||
const fs = require('node:fs'); | ||
|
||
const scrapeHtml = async () => { | ||
const configuration = dhalang.getConfiguration(); | ||
|
||
let browser; | ||
try { | ||
browser = await dhalang.launchPuppeteer(configuration); | ||
const page = await browser.newPage(); | ||
await dhalang.configure(page, configuration.userOptions); | ||
await dhalang.navigate(page, configuration); | ||
const html = await page.content(); | ||
fs.writeFileSync(configuration.tempFilePath, html); | ||
} catch (error) { | ||
console.error(error.message); | ||
process.exit(1); | ||
} finally { | ||
if (browser) { | ||
browser.close(); | ||
} | ||
process.exit(0); | ||
} | ||
}; | ||
scrapeHtml(); |
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,17 @@ | ||
require 'rspec' | ||
require 'Dhalang' | ||
|
||
describe '#html' do | ||
context 'url without specified protocol' do | ||
it 'raises InvalidURIError' do | ||
expect { Dhalang::Scraper.html("google.com") }.to raise_error(URI::InvalidURIError) | ||
end | ||
end | ||
|
||
context 'valid url' do | ||
it 'returns scraped html' do | ||
html = Dhalang::Scraper.html("https://www.google.com") | ||
expect(html.empty?).to be false | ||
end | ||
end | ||
end |