[英]Google Apps Script login to website with HTTP request
我在過去一個月里做了很多這樣的事情,所以我應該能夠幫助你,我們試圖模仿這里的瀏覽器行為,所以首先你需要使用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匹配)。 下一步完全依賴於網站,所以我將舉一個例子
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())
現在應該記錄該文件,然后您可以使用hte GAS內置函數解析csv並將數據轉儲到電子表格中。
需要注意的幾點:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.