简体   繁体   English

Google Apps脚本可以逐行读取txt吗?

[英]Can Google Apps Script read txt line by line?

First, I want parse a html and fetch some line 首先,我想解析一个HTML并获取一些行

with Google Apps Script, and it's showed 使用Google Apps脚本,它就显示出来了

" The element type "link" must be terminated by the matching end-tag "/link " " “元素类型”链接“必须由匹配的结束标记”/ link“终止”

and code here 和代码在这里

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

I think because the html use html5, that GAS can't parsing, 我认为因为html使用html5,GAS无法解析,


so I try otherwise method to parsing string, (read line by line and keep lines which I need) 所以我尝试使用其他方法来解析字符串,(逐行读取并保留我需要的行)

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

but GAS hasn't Scanner, and how can I do? 但是GAS没有Scanner,我该怎么办?


In fact, I want to go this url " https://www.ptt.cc/bbs/gossiping/index.html " 事实上,我想去这个网址“ https://www.ptt.cc/bbs/gossiping/index.html

and fetch information in 并获取信息

<div class="r-ent">
...
</div>

Google Apps Script is JavaScript so you can use the split() method to split the text content into multiple lines by the newline character. Google Apps脚本是JavaScript,因此您可以使用split()方法通过换行符将文本内容拆分为多行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Apps 脚本:将软换行符替换为硬换行符 - Google Apps Script: Replace soft line break for hard line break Google Apps 脚本:TypeError:无法读取未定义的属性“值”(第 3 行,文件“代码”) - Google Apps Script: TypeError: Cannot read property 'values' of undefined (line 3, file "Code") 我如何逐行读取txt文件 - How can i read txt file line by line 在 Google 应用脚本中获取直线旋转角度 - get straight line rotation angle in Google apps script 使用 Apps 脚本从 Google 电子表格向 LINE 通知发送消息 - Send Message to LINE Notify from Google Spreadsheets with Apps Script 在 Google Apps 脚本中的文本前插入换行符 - Insert Line breaks before text in Google Apps Script 如何使用谷歌应用程序脚本和谷歌工作表数据逐行发送 html 电子邮件 - How to send html email line by line using google apps script and google sheet data 应用程序脚本与换行符连接 - apps script concatenate with line breaks Google Apps脚本中的强行换行(带有Google Form字段的Google文档可在项目符号列表中创建新项目符号) - Hard Line Break in Google Apps Script (Google Docs with Google Form Field to create new bullet in bulleted list) 如何阅读.txt文件的第一行? - How to read the first line of .txt file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM