【问题标题】:Google Apps Script regex returning null (data scraping) (google sheets)Google Apps 脚本正则表达式返回 null(数据抓取)(谷歌表格)
【发布时间】:2020-09-15 14:08:14
【问题描述】:

目标: 我们的目标是从该网站获取 Shiller PE,并将其放入每 24 小时更新一次的谷歌表格中。

网址:https://www.gurufocus.com/sector_shiller_pe.php

The table on the website with "shiller pe" values that needs to be scraped

Live values needed to be put in this google sheet

已尝试的解决方案:

  1. =IMPORTXML("URL","XPATH")

尝试在电子表格中使用此功能,但由于网站太慢而无法抓取网站

  1. Google Apps 脚本

制作此脚本以获取数据的 HTML 并返回能源部门的席勒值:

function energyFinder() {
  var url = "https://www.gurufocus.com/sector_shiller_pe.php";
  const html = UrlFetchApp.fetch(url).getContentText();
  var regex = new RegExp(/\/mq5xwwf">26<\/a><\/b><\/u><\/td><td class="text_center">([0-9][0-9].[0-9][0-9])<\/td>/);
  var e = regex.exec(html);
  return e
Logger.log(e)

}

但这只是返回空值。任何解决方案都会有所帮助,谢谢!

附言新网站,所以我还不能嵌入图片,很抱歉

【问题讨论】:

  • 您能否添加Logger.log(html) 以确保页面正在下载。

标签: google-apps-script web-scraping google-sheets


【解决方案1】:

您可能对正则表达式本身有疑问,因为26 之前没有引号。

这应该可行:

\/mq5xwwf?>26<\/a><\/b><\/u><\/td><td class='text_center'>([0-9][0-9]\.[0-9][0-9])<\/td>

【讨论】:

    猜你喜欢
    • 2013-06-02
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 2022-11-21
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多