【问题标题】:GDocs Script to fetch web info behind a form using UrlFetchApp使用 UrlFetchApp 获取表单背后的 Web 信息的 GDocs 脚本
【发布时间】:2013-05-24 12:49:03
【问题描述】:

我想使用 GDocs 获取一些 Web 数据,但数据位于表单后面,因此我需要将一些数据发布到表单以获取结果。 (所以我不能使用 ImportXML 等) 我尝试使用的函数是https://developers.google.com/apps-script/reference/url-fetch/url-fetch-app,但我真的不知道从哪里开始,因为我没有太多的 java 脚本经验。 有没有人有一个脚本,它可以在 GDocs 中使用一个 url、表单名称和要发布的数据?

【问题讨论】:

    标签: google-apps-script


    【解决方案1】:

    这里有一个这样的脚本示例:How do I use Google Apps Script to change a CSS page to one with inline styles?

    这是一个不同的应用程序,但方法适用。这是一个可以帮助您入门的骨架函数。

    payload 对象应包含您要模拟的表单数据的名称/值对。 url 应更改为与您提交表单的站点相匹配。通过调用UrlFetchApp.fetch() 发出POST 请求后,您应该能够使用来自Does Google Apps Script have something like getElementById?getElementByVal() 实用程序解析响应。

    function postForm() {
      // Generate a POST request with form data.
      var payload =
      {
        "name" : 'Anonymous Person',   // Replace with relevant name / value pairs
        "town" : "Springfield"
      };
    
      // Because payload is a JavaScript object, it will be interpreted as
      // an HTML form. (We do not need to specify contentType; it will
      // automatically default to either 'application/x-www-form-urlencoded'
      // or 'multipart/form-data')
    
      var options =
      {
        "method" : "post",
        "payload" : payload,
        "muteHttpExceptions" : true
      };
    
      var url = "http://form.somewhere.com";   // Replace as appropriate
      var response = UrlFetchApp.fetch(url, options);
    
      // Put the receieved xml response into XMLdocument format
      var xml = response.getContentText();
      var doc = XmlService.parse(xml);
    
      // Extract the details you want...
      var someData = getElementByVal( doc, 'textarea', 'name', 'text' );
    
      ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-26
      • 1970-01-01
      相关资源
      最近更新 更多