2017-08-10 77 views
0

我需要定期保存从给定网站到google doc文件的所有链接。我花了几个小时试图做到这一点,但我是一个初学者,没有为我工作。我将不胜感激任何建议。Google Apps脚本 - 如何保存从网站到Google文档文件的所有链接?

这里是我的尝试之一(但它可能是更好地忽略它,因为它没有工作,反正):

function save_links() { 
    // create a google doc file named 'links' 
    var doc = DocumentApp.create('links'); 

    // save the source code of the website in question to a string 
    var str = UrlFetchApp.fetch('https://www.the_website_in_question').getContentText(); 

    // find all links 
    var link = str.findText('https:\/\/.*\/'); 

    // save every link to the google doc file 
    while (link != null) { 

    var foundLink = link.getElement().asText(); 
    doc.getBody().appendParagraph(foundLink);  
    link = link.findText('http:\/\/.*\/', link); 

    } 
} 

回答

2

注意,谷歌Apps Script是基于JavaScript的脚本语言。 你需要使用正确的正则表达式,它应该工作:

function save_links() { 
    // create a google doc file named 'links' 
    var doc = DocumentApp.create('links'); 

    // save the source code of the website in question to a string 
    var str = UrlFetchApp.fetch('https://riyafa.wordpress.com/').getContentText(); 

    var regExp=/(?:(?:https?|ftp|file):\/\/|www\.|ftp\.)(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#\/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#\/%=~_|$?!:,.]*\)|[A-Z0-9+&@#\/%=~_|$])/igm; 
    var theResult= str.match(regExp); 

    // save every link to the google doc file 
    for(i in theResult){ 
    doc.getBody().appendParagraph(theResult[i]); 

    } 
} 
+0

非常感谢你!我不明白你的消息,但你的源代码就像一个魅力! – Jane

+0

学习javascript是消息:) –

相关问题