javascript - Google 脚本 XML 解析

标签 javascript google-apps-script xml-parsing google-sheets

我正在解析以下 XML:

<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xml:base="https://www.sportsbook.ag/rss/nfl-football" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Sportsbook.com Live Football Betting Odds RSS Feed</title>
    <link>https://www.sportsbook.ag/rss/nfl-football</link>
    <description>Football Betting Feed</description>
    <language>en</language>
          <item>
    <title>Dallas Cowboys @ Minnesota Vikings</title>
    <link>http://www.sportsbook.ag/livesports/nfl</link>
    <description>&lt;strong&gt;12-1-16 8:25 PM&lt;/strong&gt;
Bet on Dallas Cowboys &lt;a href=&quot;https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;amp;action=addBet&amp;amp;betTypeId=80&amp;amp;selection[Foot-Dalla-Minne-120116PSA]=Foot-Dalla-Minne-120116|PSA|1|100|115|-7|-115&quot;&gt;-3.5 (-115)&lt;/a&gt;
Money:&lt;a href=&quot;https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;amp;action=addBet&amp;amp;betTypeId=80&amp;amp;selection[Foot-Dalla-Minne-120116MLA]=Foot-Dalla-Minne-120116|MLA|1|100|180|0|-180&quot;&gt;-180&lt;/a&gt;
or Minnesota Vikings &lt;a href=&quot;https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;amp;action=addBet&amp;amp;betTypeId=80&amp;amp;/selection[Foot-Dalla-Minne-120116PSH]=Foot-Dalla-Minne-120116|PSH|1|100|105|7|-105&quot;&gt;3.5 (-105)&lt;/a&gt;
Money:&lt;a href=&quot;https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;amp;action=addBet&amp;amp;betTypeId=80&amp;amp;selection[Foot-Dalla-Minne-120116MLA]=Foot-Dalla-Minne-120116|MLA|1|100|180|0|-180&quot;&gt;+157&lt;/a&gt;.
Totals: &lt;a href=&quot;https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;amp;action=addBet&amp;amp;betTypeId=80&amp;amp;selection[Foot-Dalla-Minne-120116TLO]=Foot-Dalla-Minne-120116|TLO|1|100|110|89|-110&quot;&gt;Over 44.5 (-110)&lt;/a&gt;
&lt;a href=&quot;https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;amp;action=addBet&amp;amp;betTypeId=80&amp;amp;selection[Foot-Dalla-Minne-120116TLU]=Foot-Dalla-Minne-120116|TLU|1|100|110|89|-110&quot;&gt;Under 44.5 (-110)&lt;/a&gt;</description>
     <pubDate>12-1-16 8:25 PM</pubDate>
 <dc:creator />
 <guid isPermaLink="false" />
  </item>

使用以下 Google Apps 脚本:

function stugass() {
  var live = new Array();
  var url = "https://www.sportsbook.ag/rss/nfl-football";
  var parameters = {method : "get", payload : ""};
  var xml = UrlFetchApp.fetch(url, parameters).getContentText();
  var document = XmlService.parse(xml);
  var games = document.getRootElement().getChild('channel').getChildren('item');
  if(document == null) {
    document.getRootElement().getChild('channel').getChildren('item');
  }
  for (var i=0; i < games.length-1; i++) {
    vegas = [];
    var game = games[i];
    vegas.push(game.getChildText('title'));
    vegas.push(game.getChildText('link'));
    vegas.push(game.getChildText('description'));


    live.push(vegas);
    }
    return live;
 }

如何将“描述”标记的“BLOB”部分拆分到 Google 电子表格中的多个单元格中?

最佳答案

您想要将该字段拆分到多远并不明显,但这是一种方法,使用带有正则表达式参数的 split 字符串方法,该方法按该描述中找到的 HTML 标签进行拆分。此方法返回的数组经过过滤以消除任何仅包含空格的部分。

var description = game.getChildText('description').split(/<\/?strong>|<a href="|">[^<]*<\/a>/);
vegas = vegas.concat(description.filter(function (a) {return a.trim().length;}));
live.push(vegas);

输出看起来像

12-1-16 8:25 PM  |   Bet on Dallas Cowboys   |  https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;action=addBet&amp;betTypeId=80&amp;selection[Foot-Dalla-Minne-120116PSA]=Foot-Dalla-Minne-120116|PSA|1|100|105|-7|-105   |  ... 

关于javascript - Google 脚本 XML 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40921605/

相关文章:

perl - 将 OWL 本体转换为 OBO

java - 从 URL 返回的 XML 未正确解析

c# - 如何使用 C# 从 xml 文档中删除重复元素而不用乱写 xml 结构

javascript - JSDoc:如何记录具有动态和固定属性混合的对象?

javascript - 使用 multer 上传 NodeJS 文件

javascript - AngularJS - 获取多个 JSON 键值,然后将它们相加

google-apps-script - 类型错误 : Cannot read property "range" from undefined

javascript - 如何返回对 JSONP 请求的即时响应并在之后继续处理

javascript - 数组显示其长度为 0 但控制台显示否则

google-apps-script - 将多行单元格拆分为不同的行