【问题标题】:Pulling price from amazon rss feed embedded in description从描述中嵌入的亚马逊 RSS 提要中提取价格
【发布时间】:2011-05-16 04:28:21
【问题描述】:

我正在处理一个 RSS 提要,它从亚马逊 RSS 提要中提取数据。我正在使用 C# .NET Compact Framework 3.5。我可以从 RSS 提要中的节点获取书名、出版日期等。但是,这本书的价格嵌入在描述节点中的一大堆 HTML 中。我将如何仅提取价格而不提取 HTML 负载?

if (nodeChannel.ChildNodes[i].Name == "item")
{
    nodeItem = nodeChannel.ChildNodes[i];
    row = new ListViewItem();
    row.Text = nodeItem["title"].InnerText;
    row.SubItems.Add(nodeItem["description"].InnerText);
    listBooks.Items.Add(row);
}

描述节点中间的价格示例

<description><![CDATA[    <div class="hreview" style="clear:both;">  <div class="item">        <div style="float:left;" class="tgRssImage"><a class="url" href="https://rads.stackoverflow.com/amzn/click/com/B0013FDM7E" rel="nofollow noreferrer"><img src="http://ecx.images-amazon.com/images/I/51MvRlzFlpL._SL160_SS160_.jpg" width="160" alt="I Am Legend (Widescreen Single-Disc Edition)" class="photo" height="160" border="0" /></a></div>    <span class="tgRssTitle fn summary">I Am Legend (Widescreen Single-Disc Edition) (<span class="tgRssBinding">DVD</span>)<br />By <span class="tgRssAuthor">Will Smith</span><br /></span>  </div>  <div class="description">    <br />    <span style="display: block;" class="tgRssPriceBlock"><span class="tgProductPriceLine"><a href="https://rads.stackoverflow.com/amzn/click/com/B0013FDM7E" rel="nofollow noreferrer">Buy new</a>: <span class="tgProductPrice">$5.49</span></span><br /><span class="tgProductUsedPrice"><a href="http://www.amazon.com/gp/offer-listing/B0013FDM7E/ref=tag_rso_rs_eofr_used" id="tag_rso_rs_eofr_used">285 used and new</a> from <span class="tgProductPrice">$1.00</span></span><br /></span>    <span class="tgRssReviews">Customer Rating: <img src="http://g-ecx.images-amazon.com/images/G/01/x-locale/common/customer-reviews/stars-3-5._V192240731_.gif" width="64" alt="3.6" align="absbottom" height="12" border="0" /><br /></span>    <br />    <span class="tgRssProductTag"></span>    <span class="tgRssAllTags">Customer tags: <a href="http://www.amazon.com/tag/science%20fiction/ref=tag_rss_rs_itdp_item_at">science fiction</a>(92), <a href="http://www.amazon.com/tag/will%20smith/ref=tag_rss_rs_itdp_item_at">will smith</a>(79), <a href="http://www.amazon.com/tag/horror/ref=tag_rss_rs_itdp_item_at">horror</a>(51), <a href="http://www.amazon.com/tag/action/ref=tag_rss_rs_itdp_item_at">action</a>(43), <a href="http://www.amazon.com/tag/adventure/ref=tag_rss_rs_itdp_item_at">adventure</a>(34), <a href="http://www.amazon.com/tag/fantasy/ref=tag_rss_rs_itdp_item_at">fantasy</a>(33), <a href="http://www.amazon.com/tag/dvd/ref=tag_rss_rs_itdp_item_at">dvd</a>(30), <a href="http://www.amazon.com/tag/movie/ref=tag_rss_rs_itdp_item_at">movie</a>(20), <a href="http://www.amazon.com/tag/zombies/ref=tag_rss_rs_itdp_item_at">zombies</a>(14), <a href="http://www.amazon.com/tag/i%20am%20legend/ref=tag_rss_rs_itdp_item_at">i am legend</a>(6), <a href="http://www.amazon.com/tag/bad%20sci-fi/ref=tag_rss_rs_itdp_item_at">bad sci-fi</a>(4), <a href="http://www.amazon.com/tag/mutants/ref=tag_rss_rs_itdp_item_at">mutants</a>(4)<br /></span>  </div></div>]]></description>

5.49 美元在某处乱七八糟

【问题讨论】:

  • 你能举一个包含价格的HTML代码的例子吗?

标签: c# rss amazon


【解决方案1】:

这可能是个愚蠢的想法,但是在class="tgProductPrice"&gt; 之后进行字符串搜索怎么样?然后提取followign char直到你打到结束标签&lt;/span&gt;

您不需要加载任何 html,您已经在描述中拥有它。

这对你有用吗?

【讨论】:

    【解决方案2】:

    那个描述看起来很糟糕,如果你没有任何可能获得该 RSS 提要的不同版本,我认为唯一的解决方案是解析你在描述中的 HTML。

    为此,您可以使用HTML Agility Pack(尚未使用它,但它是从 .NET 解析 HTML 的推荐解决方案)或使用正则表达式或文本搜索来查找该标签并提取价格(此我觉得有点 hacky,如果 RSS 发生变化,可能会导致需要进行许多更改)

    编辑:我已经将字符串搜索与正则表达式相结合,而维护起来是一场噩梦,但考虑到您的情况并且它仅适用于一个值,它可能没问题。

    【讨论】:

      【解决方案3】:
      using CsQuery; //get CsQuery from nuget packages
      path = textBox1.Text;
              var dom = CQ.CreateFromUrl(path);
              var divContent = dom.Select("#priceblock_ourprice").Text();
              //priceblock_ourprice is an id of span where price is written
              label1.Text = divContent.ToString();
      

      【讨论】:

        猜你喜欢
        • 2012-07-27
        • 1970-01-01
        • 2016-03-02
        • 1970-01-01
        • 2018-01-13
        • 2015-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多