Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Google Apps Script read txt line by line?

First, I want parse a html and fetch some line

with Google Apps Script, and it's showed

" The element type "link" must be terminated by the matching end-tag "/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,


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?


In fact, I want to go this url "https://www.ptt.cc/bbs/gossiping/index.html"

and fetch information in

<div class="r-ent">
...
</div>
like image 701
JimmyHo Avatar asked Oct 26 '25 05:10

JimmyHo


1 Answers

Google Apps Script is JavaScript so you can use the split() method to split the text content into multiple lines by the newline character.

var text = UrlFetchApp.fetch(url).getContentText();
var lines = text.split(/\r?\n/);
Logger.log(lines);
like image 90
Amit Agarwal Avatar answered Oct 28 '25 21:10

Amit Agarwal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!