简体   繁体   English

当 web 使用 Cheerio 报废时,如何评估 javascript 变量

[英]How do I evaluate a javascript variable when web scrapping using cheerio

When I used Cheerio to scrape https://www.bankofamerica.com/home-equity/assumptions-home-equity/?loanType=homeEquity&state=CA , I only receive a variable name instead of the variable value.当我使用 Cheerio 抓取https://www.bankofamerica.com/home-equity/assumptions-home-equity/?loanType=homeEquity&state=CA时,我只收到一个变量名而不是变量值。

Code:代码:

'''
const BankofAmericaScraper = async (browser) => {
  const date = new Date().toLocaleDateString();
  const page = await browser.newPage();
  await page.goto(URL, {
    waitUntil: ["load"],
    timeout: 0,
  });
  const MortgagesPage = await page.content();
  const $ = cheerio.load(MortgagesPage);
  const step1 = Object.values($(".col-num-2")[2])[5];
  console.log(step1);
'''

I get {{ percentage rates.product.currentRate }} and not 6.650.我得到 {{ percent rates.product.currentRate }} 而不是 6.650。 How do I access the variable?如何访问变量? I'm using a headless browser to evaluate it.我正在使用无头浏览器来评估它。

Short answer: With Cheerio, you cant简短的回答:有了 Cheerio,你不能

So right off the bat, Cheerio documentation states所以马上开始,Cheerio 文档指出

Cheerio parses markup and provides an API for traversing/manipulating the resulting data structure. Cheerio 解析标记并提供 API 用于遍历/操作生成的数据结构。 It does not interpret the result as a web browser does.它不会像 web 浏览器那样解释结果。 Specifically, it does not produce a visual rendering, apply CSS, load external resources, or execute JavaScript具体来说,它不产生视觉渲染,应用 CSS,加载外部资源,或执行 JavaScript

The trade off for this is having that speed of returning data, in comparison to other libraries that emulate the page.与其他模拟页面的库相比,这样做的代价是具有返回数据的速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM