在 Google Apps 脚本中隔离获取页面的主体
isolate the body of a fetched page in Google Apps Script
我只需要在获取页面后保留页面的正文内容。下面的代码不起作用(也就是说,html
变量在 .replace
代码行之后没有改变,正如我从日志中看到的那样)。怎么了?
var response = UrlFetchApp.fetch('
var html=response.getContentText();
html=html.replace(/.*(<body[^>]*)/m, '');
html=html.replace(/<\/body>.*/m, '</body>');
Logger.log(html);
试试这个:
function getBody(html) {
var body=html.slice(html.indexOf('<body')+'<body>'.length,html.indexOf('</body'));
Logger.log(body);
return body;
}