gpt4 book ai didi

r - 在多个属性上使用 XPathSApply 条件提取数据

转载 作者:行者123 更新时间:2023-12-04 10:53:08 25 4
gpt4 key购买 nike

以下网址包含数字和表格,我喜欢阅读表格的前两列。 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>

我需要从两个单元格中读取值,其中一个单元格对应于日期,另一个单元格对应于流量排放,并具有以下提到的属性
<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)

他们成功提取了信息,但提取的值包含来自其他表/单元格的值,重要的是“日期”和“流量”不对应。

dates[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] "31-May-98" "6-Jan-98" "30-Jun-98"



“31-Mar-98”介于“3-Jan-98”和“4-Jan-98”之间 - 一些意外

streamflowdischarge[1:10] [1] "3108" "3076" "3051" "3111" "3064" "3043" "3007" "3066" "378" ""



“3108”不对应于“1-Jan-98” - 可以在网址上查看

看起来有具有相同属性的表/单元格,我不想读取/抓取。在这方面,我认为我需要传递整个属性,即
<td height="20" class="xl6521398" align="right" style="height:15.0pt">

获取“日期”,并且以某种方式我应该设置条件,以便从同一个表中提取“流量排放”。

非常感谢建议,以及是否有其他选择。

我试过 readHTMLTable,但得到一个错误“下标越界”

谢谢,
萨蒂什

最佳答案

我输入数据

url = "http://floodobservatory.colorado.edu/SiteDisplays/1544data.htm"
html = htmlParse(url)

然后查询包含您感兴趣的两个单元格类的表格行,取每个单元格的第一个或第二个单元格
query = "//tr[./td[@class='xl6521398'] and ./td[@class='xl7321398']]/td[1]"
dates = xpathSApply(html, query, xmlValue)
query = "//tr[./td[@class='xl6521398'] and ./td[@class='xl7321398']]/td[2]"
flows = xpathSApply(html, query, xmlValue)

这些是我认为你想要的
> df = data.frame(dates=as.Date(dates, "%e-%b-%y"), flows=as.integer(flows))
> nrow(df)
[1] 5808
> head(df, 3)
dates flows
1 1-Jan-98 1258
2 2-Jan-98 1584
3 3-Jan-98 1272
> tail(df, 3)
dates flows
5806 23-Nov-13 2878
5807 24-Nov-13 2852
5808 25-Nov-13 2738

我想秘诀是对感兴趣的两列使用行的选择(??但也许这些是由用于制作网页的电子表格生成的类,与数据的语义无关?)对数据进行分组。更“完整”的抓取可能会创建一个行的节点集,然后查询标有感兴趣类别的行(有时是几个)列,例如,
query = "//tr[./td[@class='xl6521398'] and ./td[@class='xl7321398']]"
nodes = getNodeSet(html, query)
date = lapply(nodes, xpathSApply, "./td[@class='xl6521398']", xmlValue)
flow = lapply(nodes, xpathSApply, "./td[@class='xl7321398']", xmlValue)

日期和流量元素是协调的,但每个日期可以有多个流量测量值。
> head(flow, 3)
[[1]]
[1] "1258" "" "1799" "2621" "1258"

[[2]]
[1] "1584" "" "1550" "2033" "978"

[[3]]
[1] "1272" "" "1104" "3515" "233"

> table(sapply(flow, length))

2 3 5
5577 15 216

所以我猜这是针对苏丹的青尼罗河;整洁的
url = "http://floodobservatory.colorado.edu/SiteDisplays/Summary5.htm"
sites = htmlParse(url)

> sites["//tr[./td[1] = '1544']"]
[[1]]
<tr height="17" style="height:12.75pt"><td height="17" class="xl7226158" style="height:12.75pt">1544</td>&#13;
<td class="xl6926158"/>&#13;
<td class="xl7026158">13.0940</td>&#13;
<td class="xl7026158">33.9750</td>&#13;
<td class="xl6926158">5070</td>&#13;
<td class="xl6926158">Blue Nile</td>&#13;
<td class="xl6926158">Sudan</td>&#13;
<td class="xl6926158">2</td>&#13;
<td class="xl6926158">2</td>&#13;
<td class="xl7926158">173%</td>&#13;
<td class="xl8226158">15.88</td>&#13;
<td class="xl7126158">19-Nov-14</td>&#13;
<td class="xl7126158"/>&#13;
</tr>

attr(,"class")
[1] "XMLNodeSet"

关于r - 在多个属性上使用 XPathSApply 条件提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27028100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com