我正在解析以下 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>
使用以下 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&action=addBet&betTypeId=80&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/