过去一个月以来,我已经做了很多类似的事情,所以应该能够帮助您。我们正在尝试模拟浏览器的行为,因此首先需要使用Chrome开发者工具(或类似工具)记录浏览器执行的确切操作,例如提交的表单值、调用的URL等等。以下示例展示了要使用的一般技术:
第一步是登录网站并获取会话cookie:
var payload =
{
"user_session[email]" : "username",
"user_session[password]" : "password",
};// The actual values of the post variables (like user_session[email]) depends on the site so u need to get it either from the html of the login page or using the developer tools I mentioned.
var options =
{
"method" : "post",
"payload" : payload,
"followRedirects" : false
};
var login = UrlFetchApp.fetch("https://www.website.com/login" , options);
var sessionDetails = login.getAllHeaders()['Set-Cookie'];
我们已经登录到网站(为了确认,请仅记录sessionDetails并将其与Chrome设置的cookie进行匹配)。下一步完全取决于该网站,因此我将给出一个一般性的示例。
我们已经登录到网站(为了确认,请仅记录sessionDetails并将其与Chrome设置的cookie进行匹配)。下一步完全取决于该网站,因此我将给出一个一般性的示例。
var downloadPayload =
{
"__EVENTTARGET" : 'ctl00$ActionsPlaceHolder$exportDownloadLink1',
};// This is just an example it may or may not be needed, if needed u need to trace the values from the developer tools.
var downloadCsv = UrlFetchApp.fetch("https://www.website.com/",
{"headers" : {"Cookie" : sessionDetails},
"method" : "post",
"payload" : downloadPayload,
});
Logger.log(downloadCsv.getContentText())
现在应该已经记录了文件,您可以使用GAS内置函数解析csv并将数据转储到电子表格中。
需要注意以下几点: