谷歌脚本XML解析
问题描述:
我解析以下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><strong>12-1-16 8:25 PM</strong>
Bet on Dallas Cowboys <a href="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|115|-7|-115">-3.5 (-115)</a>
Money:<a href="https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;action=addBet&amp;betTypeId=80&amp;selection[Foot-Dalla-Minne-120116MLA]=Foot-Dalla-Minne-120116|MLA|1|100|180|0|-180">-180</a>
or Minnesota Vikings <a href="https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;action=addBet&amp;betTypeId=80&amp;/selection[Foot-Dalla-Minne-120116PSH]=Foot-Dalla-Minne-120116|PSH|1|100|105|7|-105">3.5 (-105)</a>
Money:<a href="https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;action=addBet&amp;betTypeId=80&amp;selection[Foot-Dalla-Minne-120116MLA]=Foot-Dalla-Minne-120116|MLA|1|100|180|0|-180">+157</a>.
Totals: <a href="https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;action=addBet&amp;betTypeId=80&amp;selection[Foot-Dalla-Minne-120116TLO]=Foot-Dalla-Minne-120116|TLO|1|100|110|89|-110">Over 44.5 (-110)</a>
<a href="https://www.sportsbook.ag/sbk/sportsbook4/www.sportsbook.ag/updatecart5.cgi?sportTypeId=203&amp;action=addBet&amp;betTypeId=80&amp;selection[Foot-Dalla-Minne-120116TLU]=Foot-Dalla-Minne-120116|TLU|1|100|110|89|-110">Under 44.5 (-110)</a></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中标记多个单元格?
答
您想要分割该字段的距离并不明显,但这里有一种方法可以使用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&action=addBet&betTypeId=80&selection[Foot-Dalla-Minne-120116PSA]=Foot-Dalla-Minne-120116|PSA|1|100|105|-7|-105 | ...
答
你可以把它简单的是这样的:
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;
}
输出看起来是这样的:
真棒!谢谢!我一直在使用你给我的东西来获得不同的输出。但是,如何获得标签内的结果...例如 Need This? –