代码之家  ›  专栏  ›  技术社区  ›  Athar K

解码/unescape Unicode Google应用程序脚本

  •  1
  • Athar K  · 技术社区  · 6 年前

    我是新解码,因此我无法解码以下响应时,使用urlfetchapp。

    下面是我用来获取数据的函数,但是我需要用Unicode来解码这些值。我能用什么函数来解码这个?

    function scrap() {
    
      var url = UrlFetchApp.fetch("https://immi.homeaffairs.gov.au/visas/working-in-australia/skillselect/invitation-rounds");
      var elements =/id="ctl00_PlaceHolderMain_PageSchemaHiddenField_Input" (.*?)\/>/gim;
    
      var x = url.getContentText().match(elements);
    
      Logger.log(x);
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Tanaike    6 年前

    虽然我不确定这是否是最好的方法,但是这次修改怎么样?

    修改的脚本:

    function scrap() {
      var url = UrlFetchApp.fetch("https://immi.homeaffairs.gov.au/visas/working-in-australia/skillselect/invitation-rounds");
      var elements =/id="ctl00_PlaceHolderMain_PageSchemaHiddenField_Input" (.*?)\/>/gim;
      var x = url.getContentText().match(elements);
    
      var res = unescape(x[0].replace(/\\u/g, "%u")); // Added
    
      Logger.log(res)
    }
    

    结果:

    当使用上面修改过的脚本作为示例时,这些值将按以下方式转换。

    来自:
    \u003cp\u003eThe table below shows the number of invitations issued in the SkillSelect invitation round on 11 September 2018.\u003c/p\u003e\n\n\u003ch3\u003eInvitations issued on 11 September 2018\u003c/h3\u003e\n\n
    
    到:
    <p>The table below shows the number of invitations issued in the SkillSelect invitation round on 11 September 2018.</p>\n\n<h3>Invitations issued on 11 September 2018</h3>\n\n
    

    参考文献:

    如果我误解了你的问题,我很抱歉。

    推荐文章