不要使用解析器跨度

Don't use Parser span

我想要获取数据(“折扣”)形式 url = https://www.lazada.sg/puma-singapore/?q=All-Products&from=wangpu&langFlag=en&pageTypeId=2

但没有得到

function myFunction() {
 const url = 'https://www.lazada.sg/puma-singapore/?q=All-Products&from=wangpu&langFlag=en&pageTypeId=2'
  // parse the data 
    

function getData(url) {
    const fromText = '<span class="IcOsH" data-spm-anchor-id="a2o42.seller.list.i41.62ff63deVng91O">';
    const toText = '</span>';
    const content = UrlFetchApp.fetch(url).getContentText();
    const scraped = Parser
                    .data(content)
                    .setLog()     
                    .from(fromText)
                    .to(toText)
                    .build();
    return scraped;
}

  const discount = getData(url).replace("%", "").replace(/\-/g,"");
  Logger.log(discount)
}

当我看到 URL 的 HTML 时,似乎是使用 Javascript 放置值。但是,幸运的是,这些值作为 JSON 数据包含在 HTML 中。因此,在这个答案中,我想建议通过解析 HTML 中的 JSON 数据来检索值。示例脚本如下

示例脚本:

请设置您要检索discount值的项目名称。

function myFunction() {
  const itemName = "PUMA Unisex Deck Backpack II"; // Please set the item name.

  const url = 'https://www.lazada.sg/puma-singapore/?q=All-Products&from=wangpu&langFlag=en&pageTypeId=2'
  const content = UrlFetchApp.fetch(url).getContentText();
  const str = content.match(/window.pageData =([\w\s\S]+?});/);
  if (!str || str.length < 1) return;
  const obj = JSON.parse(str[1]);
  const items = obj.mods.listItems.filter(({ name }) => name == itemName);
  if (items.length == 0) return;
  const res = items.map(({ discount }) => discount);
  console.log(res)
}

测试:

  • 当此脚本为运行时,得到[ '-34%', '-34%' ]。因为有2项PUMA Unisex Deck Backpack II。因此,结果有 2 个值。

注:

  • 在当前阶段,我可以确认此脚本有效。但是,如果将来 HTML 的结构发生变化,则此脚本可能无法使用。请注意这一点。

参考文献:

已添加:

关于我的问题 About your additional request of your comment of Thanks for the support However I want to get all the data of all products Is there any way?, you want the discount values of all items. Is my understanding correct? 您的附加请求,

yes, i got all data of discount, but i got value"undefined", i want get value form "46" or "56" and setValues on MySheet.

在这种情况下,示例脚本如下。

示例脚本:

function myFunction() {
  const url = 'https://www.lazada.sg/puma-singapore/?q=All-Products&from=wangpu&langFlag=en&pageTypeId=2'
  const content = UrlFetchApp.fetch(url).getContentText();
  const str = content.match(/window.pageData =([\w\s\S]+?});/);
  if (!str || str.length < 1) return;
  const obj = JSON.parse(str[1]);
  const items = obj.mods.listItems;
  if (items.length == 0) return;
  const res = items.map(({ discount }) => [-parseInt(discount, 10) || 0]);

  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set the sheet name.
  sheet.getRange(1, 1, res.length, res[0].length).setValues(res);
}
  • 好像discount的值为undefined时,商品没有打折。所以在这种情况下,它是 0%.

  • 并且,为了实现您想要将 -46% 的值检索为 46 的目标,我使用了 -parseInt(discount, 10) || 0.

  • 当此脚本为 运行 时,检索到的值将放入“Sheet1”的“A”列。不幸的是,从 i want get value form "46" or "56" and setValues on MySheet 开始,我无法想象您的实际目标。所以,请根据您的实际目标修改此脚本。

  • 如果要检索商品名称和折扣值,请修改如下。

    • 来自

        const res = items.map(({ discount }) => [-parseInt(discount, 10) || 0]);
      
    •   const res = items.map(({ name, discount }) => [name, -parseInt(discount, 10) || 0]);