谷歌Apps脚本登录网站与HTTP请求 [英] Google Apps Script login to website with HTTP request

查看:315
本文介绍了谷歌Apps脚本登录网站与HTTP请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的谷歌驱动器A S preadsheet,我想从其他网站下载一个CSV,放入我的小号preadsheet。现在的问题是,我要登录到网站第一次,所以我需要使用一些HTTP请求来做到这一点。

I have a spreadsheet on my Google Drive and I want to download a CSV from another website and put it into my spreadsheet. The problem is that I have to login to the website first, so I need to use some HTTP request to do that.

我发现网站和这个。如果这些网站上有答案,那么我显然不明白他们足够的数字出来。有人能帮助我想出解决办法?我觉得第二个站点是特别是接近我需要什么,但我不知道它在做什么。

I have found this site and this. If either of these sites has the answer on it, then I clearly don't understand them enough to figure it out. Could someone help me figure this out? I feel that the second site is especially close to what I need, but I don't understand what it is doing.

要再次澄清,我想通过一个HTTP请求进行登录,然后对同一网站的呼叫使用不同的URL,它是在调用来获取CSV文件。

To clarify again, I want to login with an HTTP request and then make a call to the same website with a different URL that is the call to get the CSV file.

推荐答案

我已经做了很多这在过去的一个月,所以我应该能够帮助你,我们正试图所以首先你需要在这里模拟浏览器行为使用Chrome的开发者工具(或类似的东西),并记下确切的事情浏览器确实喜欢贴在表格值,那个叫等的URL。下面的例子显示使用的一般techinique:

I have done a lot of this in the past month so I should be able to help you, we are trying to emulate the browsers behaviour here so first you need to use chrome's developer tools(or something similar) and note down the exact things the browser does like the form values posted, the url that is called and so on. The following example shows the general techinique to be used:

的第一步是登录到网站,并获得会话cookie:

The first step is to login to the website and get the session 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并与镀铬设置的cookie匹配它)。下一步是纯粹依赖于网站上,所以我将要给你一个普通的例子。

We have logged into the website (In order to confirm just log the sessionDetails and match it with the cookies set by chrome). The next step is purely dependent on the website so I will give u a general example

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和在S preadsheet转储数据。

The file should now be logged, you can then parse the csv using hte GAS inbuilt function and dump the data in the spreadsheet.

有几点要注意:


  • 我认为所有的表单提交值是静态的,可以是
    硬codeD,万一这是不正确的话让我知道,我会给你
    一个功能,可从HTML中提取的值。

  • 有些网站需要浏览器发送一个令牌值(该值将在HTML present),与凭证一起。在这种情况下,你需要提取值,然后将它张贴。

这篇关于谷歌Apps脚本登录网站与HTTP请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆