0

我解析以下XML:谷歌腳本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> 

與下列谷歌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 Spreadsheet中標記多個單元格?

回答

1

您想要分割該字段的距離並不明顯,但這裏有一種方法可以使用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 | ... 
+0

真棒!謝謝!我一直在使用你給我的東西來獲得不同的輸出。但是,如何獲得標籤內的結果...例如 Need This? –

1

你可以把它簡單的是這樣的:

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').replace(/<a href=\S+\">|<\S+>/g,'')); 


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

輸出看起來是這樣的:

enter image description here