在Google Apps脚本中隔离已获取页面的正文

问题描述 投票:0回答:1

在获取页面后,我只需要保留其正文内容。以下代码不起作用(即,从日志中可以看到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);
regex google-apps-script urlfetch
1个回答
3
投票

尝试一下:

function getBody(html) {
  var body=html.slice(html.indexOf('<body')+'<body>'.length,html.indexOf('</body'));
  Logger.log(body);
  return body;
}
© www.soinside.com 2019 - 2024. All rights reserved.