2014-10-17 86 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

相关问题