2014-10-17 35 views
2

我以Google Apps腳本編碼開始。UrlFetchApp.fetch()google.com上的不完整內容

所以,我想這個例子:fetch(url)

// The code below logs the HTML code of the Google home page. 
var response = UrlFetchApp.fetch("http://www.google.com/"); 
Logger.log(response.getContentText()); 

我自收到標籤:

<!doctype html> to <style> 

但有沒有這些標籤:

</head>, <body>, </body> or </html> 

是它一個不正確的Google Apps腳本示例,還是我的錯誤? 如何回覆google.com上的完整HTML代碼?

回答

4

Logger.log會自動截斷在特定長度後顯示的字符串。您正在接收整個頁面,但只能看到日誌中的第一部分。

// The code below logs the HTML code of the Google home page. 
var response = UrlFetchApp.fetch("http://www.google.com/"); 
var content = response.getContentText(); 
Logger.log(content); 
//log last 1000 chars of content 
Logger.log(content.substr(-1000)); 
+0

substr(-1000)正常工作。謝謝。 – Benno 2014-10-17 18:22:42

+0

@cameron Roberts。你能告訴我什麼樣的正則表達式應該用於獲得標題和URL形式content.subster(-1000)? – user1788736 2015-10-26 00:02:39