首页 > 解决方案 > 在 Google Apps 脚本中隔离已获取页面的正文

问题描述

获取页面后,我只需要保留页面的正文内容。下面的代码不起作用(也就是说,html变量在代码行之后没有改变.replace,正如我从日志中看到的那样)。怎么了?

var response = UrlFetchApp.fetch('https://stackoverflow.com/questions/58049531/another-importxml-returning-empty-content');

var html=response.getContentText();
html=html.replace(/.*(<body[^>]*)/m, '$1');  
html=html.replace(/<\/body>.*/m, '</body>');  

Logger.log(html);

标签: regexgoogle-apps-scripturlfetch

解决方案


尝试这个:

function getBody(html) {
  var body=html.slice(html.indexOf('<body')+'<body>'.length,html.indexOf('</body'));
  Logger.log(body);
  return body;
}

推荐阅读