无法获取元素JavaScript谷歌应用程序脚本 [英] Can't get element javascript google apps script
问题描述
我试图获取feed中的link元素,我可以获取 description
和 title
,但无法获取链接
元素。这对我来说似乎很奇怪。这是我的代码
I'm trying to get the link element in a feed, i can get the description
and title
, but cannot get the link
element. It seems weird to me. Here is my code
var url = "http://healthyhow.net/feed";
var response = UrlFetchApp.fetch(url);
var shareDoc = Xml.parse(response.getContentText(), true);
var root = shareDoc.getElement(); // feed element
var entries = root.getElement("channel").getElements("item");
for (var i=0; i<1; i++) { //just pick the first entry
var e = entries[i];
var title = e.getElement("title").getText();
var link = e.getElement("link").getText();
var description = e.getElement("description").getText();
}
有人可以指出这里有什么问题吗?
Thanks!
Could anyone point out what is wrong here? Thanks!
推荐答案
docs 表明您应该对HTML进行宽松解析 - 目前尚不清楚这是在底层做什么,但在您的情况下,可能会令HTML <使用具有相同标签名称的通用XML元素链接>
元素。它似乎将链接条目解析为类似于您的代码的内容(您可以在 shareDoc.toXmlString()
)中看到:
The docs indicate you should use lenient parsing for HTML - it's unclear what this is doing underneath, but in your case maybe it's confusing an HTML <link>
element with a generic XML element with that same tag name. It appears to parse the link entries into something like this for your code (which you can see in shareDoc.toXmlString()
):
<link/>http://healthyhow.net/l-arginine-natural-treatment-for-hypertension/
由于它是一个空标记,因此没有文字。
Since it's an empty tag, no text.
更改:
Change:
var shareDoc = Xml.parse(response.getContentText(), true);
为:
to be:
var shareDoc = Xml.parse(response.getContentText(), false);
,您应该可以获取链接文本。
and you should be able to get the link text.
这篇关于无法获取元素JavaScript谷歌应用程序脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!