【问题标题】:Scrape Data from "div" class从“div”类中抓取数据
【发布时间】:2012-02-22 14:38:02
【问题描述】:

我尝试并能够使用以下脚本从 td 类中抓取数据:

 nArticles <- getNodeSet(pagetree,"//*/td[@class='bg1 W1']//*/li[@class='LI2 font28 C bold W1']") #current price
 current.price <- xmlValue(nArticles[[1]])

现在我有一个如下的网络资源:

<div>
    <div style="float: left;">
            <ul class="BlockItemIndex" style="width:123px; height:92px">
                    <li class="font12 I1">
                            Index
                    </li>
                    <li class="I1" style="font:bold 20px Arial">
                            <span id="ctl00_ctl00_cphContent_cphContent_lblIndex">21,549.28</span></li>
                    <li class="I1" style="font:normal 15px Arial">
                            <span id="ctl00_ctl00_cphContent_cphContent_lblChange"><span class="pos bold">+70.56 (0.33%)</span></span></li>
                    <li class="I1">
                            <span class="font12">Turnover</span>&nbsp;<span id="ctl00_ctl00_cphContent_cphContent_lblTurnover">70.41B</span></li>
            </ul>
    </div>
    <div class="seperate"></div>
    <div style="float: left;">
            <ul class="BlockItemChange" style="width:75px">
                    <li class="font12 I1">
                            High
                    </li>
                    <li class="I2">
                            <span id="ctl00_ctl00_cphContent_cphContent_lblHigh">21,569.74</span></li>
            </ul>
            <ul class="BlockItemChange" style="width:75px; margin-top:2px;">
                    <li class="font12 I1">
                            Low
                    </li>
                    <li class="I2">
                            <span id="ctl00_ctl00_cphContent_cphContent_lblLow">21,302.19</span></li>
            </ul>
    </div>
    <div class="seperate"></div>
    <div style="float: left;">
            <ul class="BlockItemChange" style="width:75px">
                    <li class="font12 I1">
                            Open
                    </li>
                    <li class="I2">
                            <span id="ctl00_ctl00_cphContent_cphContent_lblOpen">21,339.02</span></li>
            </ul>
            <ul class="BlockItemChange" style="width:75px; margin-top:2px;">
                    <li class="font12 I1">
                            Prev Close
                    </li>
                    <li class="I2">
                            <span id="ctl00_ctl00_cphContent_cphContent_lblPreClose">21,478.72</span></li>
            </ul>
    </div>
</div>

我需要接21,549.28,我尝试了以下方法:

nArticles <- getNodeSet(pagetree,"//*/ul[@class='BlockItemChange']//*/li[@class='I2']") 

但是失败了。任何人都可以帮忙吗?谢谢。

【问题讨论】:

    标签: xml r web-scraping


    【解决方案1】:

    很难知道您使用什么来确定您感兴趣的价值,但是

    query = '//ul[@class="BlockItemIndex"]/li[2]/span/text()'
    xpathSApply(xml, query, xmlValue)
    

    挑选出所有具有至少两个 li 元素且包含 span 元素的 BlockItemIndex 元素。由于所有 li 元素都具有相同的类,因此指定一个类没有帮助。我不确定你想用* 完成什么;我认为// 是多余的。稍后在您的查询中,// 不是您想要的,您对 BlockItemClass 元素的直接后代感兴趣。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-09
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      相关资源
      最近更新 更多