【问题标题】:Can Google Apps Script read txt line by line?Google Apps 脚本可以逐行读取 txt 吗?
【发布时间】:2017-06-08 09:51:24
【问题描述】:

首先,我想解析一个 html 并获取一些行

使用 Google Apps 脚本,它已显示

" 元素类型 "link" 必须以匹配的结束标签 "/link " " 结束

这里是代码

var response = UrlFetchApp.fetch(url)
var downloadContent = response.getContentText();
var doc = XmlService.parse(downloadContent);

我认为因为html使用html5,GAS无法解析,


所以我尝试了其他方法来解析字符串, (逐行阅读并保留我需要的行)

var xml = UrlFetchApp.fetch(url).getContentText();

但是 GAS 没有 Scanner,我该怎么办?


其实我想去这个网址“https://www.ptt.cc/bbs/gossiping/index.html

并在

中获取信息
<div class="r-ent">
...
</div>

【问题讨论】:

标签: javascript html parsing google-apps-script fetch


【解决方案1】:

Google Apps 脚本是 JavaScript,因此您可以使用 split() 方法通过换行符将文本内容拆分为多行。

var text = UrlFetchApp.fetch(url).getContentText();
var lines = text.split(/\r?\n/);
Logger.log(lines);

【讨论】:

    猜你喜欢
    • 2013-09-28
    • 1970-01-01
    • 2012-02-16
    • 2012-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-10
    相关资源
    最近更新 更多