regex - 在 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);
解决方案
尝试这个:
function getBody(html) {
var body=html.slice(html.indexOf('<body')+'<body>'.length,html.indexOf('</body'));
Logger.log(body);
return body;
}
推荐阅读
- react-native - undefined 不是对象(评估 'event.target.value.toLowerCase')
- ruby - 从打开的浏览器获取 URL
- excel - 如何在多个单元格中同时使用 SUM 和 VLOOKUP 公式?
- angular - 防止引导程序 4 将自动活动类添加到复选框按钮
- c# - 如何从字节数组播放视频,并且不允许用户获取原始文件?
- python - 在写入时使用 pandas 读取 csv 文件
- asp.net - 在 UserControl 中填充复选框列表需要很长时间
- manifest - 无法安装具有移动支持的 Outlook 插件清单
- javascript - 使用 colspan 定位 Sticky?
- python-3.x - Python3 goto赋值为无限while循环?