initial working version
This commit is contained in:
64
src/components/Scraper.js
Normal file
64
src/components/Scraper.js
Normal file
@@ -0,0 +1,64 @@
|
||||
import jsdom from "jsdom";
|
||||
import Car from "../model/Car";
|
||||
import Selectors from '../util/Selectors';
|
||||
|
||||
const {
|
||||
JSDOM
|
||||
} = jsdom;
|
||||
|
||||
class Scraper {
|
||||
document;
|
||||
car;
|
||||
|
||||
constructor() {}
|
||||
|
||||
setContent(text) {
|
||||
const parsedContent = new JSDOM(text).window.document;
|
||||
if (parsedContent.querySelector(Selectors.properties.main.container) === null) {
|
||||
throw Error('No data was received. Cookie is probably expired.')
|
||||
}
|
||||
this.document = parsedContent;
|
||||
}
|
||||
|
||||
getTextBySelector(selector) {
|
||||
return this.document.querySelector(selector).innerHTML;
|
||||
}
|
||||
|
||||
scrapeMainProperties() {
|
||||
const {
|
||||
main: selector
|
||||
} = Selectors.properties;
|
||||
this.document
|
||||
.querySelector(selector.container)
|
||||
.querySelectorAll(selector.rows)
|
||||
.forEach((field) => {
|
||||
const value = field.querySelectorAll(selector.cell);
|
||||
let data;
|
||||
if (value[1].childElementCount > 0) {
|
||||
data = value[1].querySelector(selector.irregularText).innerHTML;
|
||||
} else {
|
||||
data = value[1].innerHTML;
|
||||
}
|
||||
this.car[value[0].innerHTML] = data;
|
||||
})
|
||||
}
|
||||
|
||||
scrapeBasicProperties() {
|
||||
if (!this.document) {
|
||||
throw Error('No data to scrape.');
|
||||
}
|
||||
const properties = this.document.querySelector('#content');
|
||||
const {
|
||||
properties: selector
|
||||
} = Selectors;
|
||||
const plate = this.getTextBySelector(selector.plate);
|
||||
const carName = this.getTextBySelector(selector.name);
|
||||
const vin = this.getTextBySelector(selector.vin);
|
||||
console.log(plate, carName, vin);
|
||||
this.car = new Car(plate, carName, vin.substring(5));
|
||||
return this.car;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default Scraper;
|
||||
Reference in New Issue
Block a user