【问题标题】:Extract XML element value from a RSS feed从 RSS 提要中提取 XML 元素值
【发布时间】:2014-11-21 17:59:21
【问题描述】:

我有一个 RSS 提要,我需要从中提取最新的 pubDate 元素以进行测试。最好的方法是什么?

RSS 订阅链接:https://secure.hyper-reach.com/rss/310085

示例 XML:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <atom:link href="https://secure.hyper-reach.com/rss/310085" rel="self" type="application/rss+xml" />
        <link>https://secure.hyper-reach.com/rss/310085</link>
        <title>Hyper-Reach Automated Test Account alerts feed "Automated RSS Test"</title>
        <description>Constant feed of alerts from Automated Test Account via hyper-reach.com</description>
        <lastBuildDate>Fri, 21 Nov 2014 00:56:15 -0500</lastBuildDate>
        <language>null</language>
        <ttl>5</ttl>
        <item>
            <title>Alert (2014-11-21)</title>
            <pubDate>Fri, 21 Nov 2014 00:56:15 -0500</pubDate>
            <description>This is a test message.</description>
            <link>https://secure.hyper-reach.com/servlet/getprompt?prompt_id=122967&amp;ver=0&amp;format=34&amp;nologin=1</link>
            <guid isPermaLink="false">https://secure.hyper-reach.com/rss/item/257029</guid>
        </item>
        <item>...</item>
        <item>...</item>
</channel>
</rss>

我在做什么:

checkRSSFeed = function() {
    //first I navigate to a certain page in my website
    var href = '';

    casper.then(function() {
        this.test.assertExists(x('//a[contains(@href, "SUBSTRING OF URL")]'), 'the element exists');
        href = casper.getElementAttribute(x('//a[contains(@href, "SUBSTRING OF URL")]'), 'href');
     }).then(function() {
        this.open(href);
     }).then(function() {
        this.echo(this.getCurrentUrl());

        var pubDate = '';
        this.getPageContent();
        pubDate = this._utils_.getElementByXPath('.//pubDate');
     });
};  

我得到的错误是

uncaughtError: TypeError: 'undefined' is not an object (evaluating 'this._utils_.getElementByXPath')

【问题讨论】:

    标签: javascript xml xpath rss casperjs


    【解决方案1】:

    要检索pubDate 内容,您可以使用casper.fetchText 函数,但它有一个缺点,它将所有文本节点连接成一个字符串:

    casper.echo(casper.fetchText("pubDate"));
    

    会打印

    2014 年 11 月 21 日星期五 00:56:15 -05002014 年 11 月 21 日星期五 00:47:34 -05002014 年 11 月 21 日星期五 00:45:36 -0500

    要实际单独检索文本,您可以使用casper.getElementsInfo,它适用于多个元素并提供text 属性。之后的简单映射会生成一个您可以在之后处理的数组:

    var pubDates = casper.getElementsInfo("pubDate").map(function(elementInfo){
        return elementInfo.text; // or even `return new Date(elementInfo.text)`
    });
    

    但由于您只想要最新的,并且 RSS 提要按从新到旧排序,因此您可以简单地使用第一个(注意 getElementInfo 中缺少 s):

    var pubDate = casper.getElementInfo("pubDate").text;
    

    如果您在页面上下文中执行此操作,您之前的方法会奏效。 clientutils 模块只能在页面上下文中访问(在casper.evaluate 内)。

    var pubDate = this.evaluate(function(){
        return __utils__.getElementByXPath('//pubDate').innerText;
    });
    

    注意__utils__ 两边都有两个下划线。此外,您不能将 DOM 元素从页面上下文传递到 casper 上下文,但您可以传递字符串和其他原始对象。因此我返回了 DOM 元素的 innerText 属性。 documentation 是这样说的:

    注意:评估函数的参数和返回值必须是简单的原始对象。经验法则:如果可以通过 JSON 序列化就可以了。

    【讨论】:

      猜你喜欢
      • 2013-04-02
      • 2018-05-01
      • 1970-01-01
      • 2011-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      相关资源
      最近更新 更多