TMD
TMD

Reputation: 66

Use google apps script to get website as html

I am trying to get the contents of a google site as html using the google apps script. However, when I use the code suggested on the website https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app#fetch(String,Object).

var response = UrlFetchApp.fetch("https://sites.google.com/a/*********.net/********/****");
Logger.log(response.getContentText());

This does not log the html of the site I am trying to reach though, it seems to return the default google sites html. I have found nothing online that would explain this, other than that the site is using "https", which the google site above says should not be a problem. I am able to get the html from other websites, including some other sites using https, so I am confused.

Any help would be greatly appreciated.

Upvotes: 4

Views: 4905

Answers (1)

Jonathan Seed
Jonathan Seed

Reputation: 2017

Try using the SitesApp API instead. Your new code would look like this.

var page = SitesApp.getPageByUrl("https://sites.google.com/a/*********.net/********/****");
var htmlContent = page.getHtmlContent();
Logger.log(htmlContent);

I think the issue in your current code has something to do with authentication. If you site is private (you have to be logged in to view it) issuing an http request without providing the Authentication details probably wouldn't work.

Upvotes: 2

Related Questions