【发布时间】:2015-01-17 15:15:18
【问题描述】:
以下网址包含数字和表格,我喜欢阅读表格的前两列。 xpatahSApply 命令工作正常,但我需要以两个以上的属性为条件,我无法弄清楚。
url ="http://floodobservatory.colorado.edu/SiteDisplays/1544data.htm"
doc=htmlTreeParse(url,useInternal=TRUE)
解析数据样本
<tr height="20" style="height:15.0pt">
<td height="20" class="xl6521398" align="right" style="height:15.0pt">11-Oct-13</td>
<td class="xl7321398">1853</td>
<td class="xl7321398"></td>
<td class="xl8121398">0.80</td>
<td class="xl7221398" align="right">4.87</td>
<td class="xl1521398"></td>
<td class="xl1521398"></td>
<td class="xl1521398"></td>
<td class="xl1521398"></td>
<td class="xl1521398"></td>
<td class="xl1521398"></td>
<td class="xl7421398"></td>
<td class="xl7421398"></td>
<td class="xl7421398"></td>
<td class="xl7421398"></td>
<td class="xl9621398"></td>
<td class="xl7421398"></td>
<td class="xl8121398"></td>
</tr>
我需要从两个单元格中读取值,其中一个对应于 date,另一个对应于 streamflow 排放,并具有以下提到的属性
<td height="20" class="xl6521398" ...> and [<td class="xl7321398"..]
关于上面的示例数据,我需要抓取“11-Oct-13”和“1853”。
我使用以下命令获取“日期”和“流量排放”。
dates=xpathSApply(doc,"//td[@class='xl6521398']",xmlValue)
streamflowdischarge=xpathSApply(doc,"//td[@class='xl7321398']",xmlValue)
他们成功提取信息,但提取的值包含来自其他表/单元格的值,重要的是“日期”和“流量排放”不对应。
日期[1:10] [1] "1-Jan-98" "2-Jan-98" "3-Jan-98" "31-Mar-98" "4-Jan-98" "30-Apr-98" "5-Jan- 98" [8]“98 年 5 月 31 日”“98 年 1 月 6 日”“98 年 6 月 30 日”
“31-Mar-98”介于“3-Jan-98”和“4-Jan-98”之间——有些意外
溪流排放[1:10] [1] “3108” “3076” “3051” “3111” “3064” “3043” “3007” “3066” “378” “”
“3108”不对应于“1-Jan-98” - 可以在 url 上查看
看起来有些表格/单元格具有相同的属性,我不想读取/抓取。在这方面,我认为我需要传递整个属性,即,
<td height="20" class="xl6521398" align="right" style="height:15.0pt">
为了获取“日期”,我应该以某种方式设置条件,以便从同一个表中提取“流量排放”。
非常感谢您的建议,以及是否有其他可用选项。
我尝试了 readHTMLTable,但收到错误“下标越界”
谢谢, 萨蒂什
【问题讨论】:
标签: r xml-parsing html-parsing