Reputation: 99
172/5000 Good afternoon! I am trying to get the value of the variable "var JS_WCACHE_CK =" inside the tag, but I have already tested and tried to adapt some codes, but without success.
<script>
var JS_IDIOMA = "pt";
var JS_LINK_ROOT = "https://tabuademares.com";
var JS_RUTA_ASSETS = "/assets/";
var CONF_FORMATO_HORA = 1;
window.google_analytics_uacct = "UA-8166479-17";
var JS_URL_ACTUAL="%2Fbr%2Fespirito-santo%2Fvitoria";
var JS_FECHA_ACTUAL="2020-01-26+19%3A00";
var JS_CODIGO_ESTACION="br56";
var JS_WCACHE_CK="Mjg5Ng==";
var JS_ACTIVAR_SERVIDOR_BACKUP=1;
var JS_LATITUD="-20.32352";
var JS_LONGITUD="-40.29919";
var JS_ZOOM="12";
</script>
Link site is: https://tabuademares.com/br/espirito-santo/vitoria
Upvotes: 0
Views: 585
Reputation: 11548
I would say Cheerio is not what you're after. A more appropriate too is Puppeteer, as you need something that not only parses the html but also has a language engine so you can interact with on page script without having to do evil things such as eval
:
import { Browser, launch, Page, Response } from "puppeteer";
export class JsVarService {
private browser!: Browser;
private page!: Page;
constructor(private url: string) {}
public async getVarValue(varName: string): Promise<string> {
await this.getResponse();
const results = <string>await this.page.evaluate(`window["${varName}"]`);
await this.close();
return results;
}
private async getResponse(): Promise<Response | null> {
this.browser = await launch();
this.page = await this.browser.newPage();
return this.page.goto(this.url);
}
private async close(): Promise<void> {
this.browser.disconnect();
}
}
async function run(): Promise<void> {
const url = "https://tabuademares.com/br/espirito-santo/vitoria";
const varName = "JS_WCACHE_CK";
const service = new JsVarService(url);
console.log(await service.getVarValue(varName));
}
run().catch(e => { throw new Error(e) });
Upvotes: 1