【问题标题】:Yahoo Pipe: How to parse sub DIVs雅虎管道:如何解析子 DIV
【发布时间】:2011-05-11 18:07:56
【问题描述】:

对于具有多个 DIV 的页面,如何仅从包含有用文本的 DIV 中获取内容并避免其他用于广告的 DIV 等。

例如这样的页面结构:

...

<div id="articlecopy">

  <div class="advertising 1">Ads I do not want to fetch.</div>

  <p>Useful texts go here</p>

  <div class="advertising 2">Ads I do not want to fetch.</div>

  <div class="related_articles_list">I do not want to read related articles so parse this part too</div>

</div>

...

在这个虚构的例子中,我想去掉广告的两个 DIV 和相关文章的 DIV。我想要的只是在

中获取有用的内容

在父 DIV 中。

Pipe 能做到这一点吗?

谢谢。

【问题讨论】:

    标签: yahoo-pipes


    【解决方案1】:

    尝试使用 xpath 的 YQL 模块。大致如下:

    SELECT * from html where url="http://MyWebPageWithAds.com" and xpath='//div/p'
    

    上述查询将检索父

    标记下

    标记内的 html 部分。如果您的 DIV 具有属性,您可以使用 xpath。

    例如,您有一个包含多个 DIV 的页面,但您想要的页面如下所示:

    <div>
        <div>Stuff I don't want</div>
        <div class="main_content">Stuff I want to add to my feed</div>
        <div>Other stuff I don't want</div> 
    </div>
    

    您可以将上面的 YQL 字符串更改为:

    SELECT * from html where url="http://MyWebPageWithAds.com" 
    and xpath='//div/div[contains(@class,"main_content")]'
    

    我自己最近才发现 YQL,并且对使用 xpaths 还很陌生,但到目前为止它对我很有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 2015-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-20
      相关资源
      最近更新 更多