gpt4 book ai didi

xml - 有没有更好的方法在 R 中完成这个 XML 抓取任务?

转载 作者:行者123 更新时间:2023-12-03 16:12:36 25 4
gpt4 key购买 nike

我有一些看起来像的 XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>bbox.NE.lat</key>
<string>-27.45433</string>
<key>bbox.NE.lon</key>
<string>153.01474</string>
<key>bbox.SW.lat</key>
<string>-27.45706</string>
<key>bbox.SW.lon</key>
<string>153.01239</string>
<key>crs</key>
<string>EPSG 4326</string>
<key>found</key>
<string>1</string>
</dict>
<array>
<dict>
<key>bbox</key>
<dict>
<key>bbox.NE.lat</key>
<string>-27.45433</string>
<key>bbox.NE.lon</key>
<string>153.01474</string>
<key>bbox.SW.lat</key>
<string>-27.45706</string>
<key>bbox.SW.lon</key>
<string>153.01239</string>
</dict>
<key>centroid</key>
<dict>
<key>lat</key>
<dict>
<key>lat</key>
<string>-27.45513</string>
<key>lon</key>
<string>153.0137</string>
</dict>
</dict>
<key>id</key>
<string>33037721</string>
<key>properties</key>
<dict>
<key>amenity</key>
<string>university</string>
<key>name</key>
<string>Queensland University of Technology</string>
<key>osm_element</key>
<string>way</string>
<key>osm_id</key>
<string>26303436</string>
</dict>
</dict>
</array>
</array>
</plist>

为了重现性,我用一个循环抓取了 XML:
# initialize the vector
queries<-(0)
# loop over coordinates - signups$latlong is just a vector of coordinates
# signups$latlong[1] = "51.5130004883%20-0.1230000034
# the space between lat & long is URL encoded
for(i in 1:length(signups$latlong)){
#self-imposed rate-limiting
Sys.sleep(0.05)
# if query returns an error, write NA and move on, also output to console so I can keep an eye on it
if(class(try(queries[i]<- getURL(paste("http://geocoding.cloudmade.com/[API KEY HERE]/geocoding/v2/find.plist?object_type=university&around=",signups$latlong[i],"&results=5&distance=closest", sep="")), silent=T))=="try-error")
{
queries[i]<-NA
print("NA")
print(i)
}
# just a progress indicator
else(print(i))
}

我只是在追求大学的名字。我对 XML 不太了解,但我发现这是有效的:
pagetree<-(0)
for (i in 1:length(queries)){
plist<-xmlTreeParse(queries[i])$doc$children$plist
nodes<-getNodeSet(plist, "//array//array//dict")
if(class(try(pagetree[i]<-xmlValue(nodes[[5]][[4]]), silent = T))=="try-error") pagetree[i]<-NA
}

但是,我看到有人提到 xmlXPathApply()我想知道解析循环是否不能重新编写以使用它。

我卡住的地方是我不知道如何为我想要的部分编写 XPath,因为有多个节点具有相同的名称。

也不要犹豫,提出更好的错误处理方法。

最佳答案

修复xml后...

使用 xpath 遍历它的方法是:

> plist = xmlParse('data.xml')
> xpathSApply(plist, '/plist/array/dict/dict/string', xmlValue)
[1] "-27.45433" "153.01474"
[3] "-27.45706" "153.01239"
[5] "university" "Queensland University of Technology"
[7] "way" "26303436"

您可以像平常一样索引输出。

但是,如果节点具有属性,例如 <string type='uniname">...</string> ,那么您可以使用漂亮的“@”语法,例如:
> xpathSApply(plist, '/plist/array/dict/dict/string[@type='uniname']', xmlValue)

另一种可能更适合这种 plist 格式的方法是:
> sapply(getNodeSet(plist, '//key[text() = "name"]'), function(x) xmlValue(getSibling(x)))
[1] "Queensland University of Technology"

关于xml - 有没有更好的方法在 R 中完成这个 XML 抓取任务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8480018/

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