有没有更好的方法在 R 中完成这个 XML 抓取任务? [英] is there a better way to do this XML scraping task in R?

查看:22
本文介绍了有没有更好的方法在 R 中完成这个 XML 抓取任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类似于以下内容的 XML:

I have some XML that looks like:

<?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:

For reproducibility's sake, I grabbed the XML with a loop:

# 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 不太了解,但我发现这行得通:

and I'm just after the name of the university. I don't know much about XML, but I've figured out that this works:

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() 并且我想知道解析循环是否无法重新编写以使用它.

However, I've seen mention of xmlXPathApply() and I am wondering if the parsing loop couldn't be re-written to use it.

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

Where I'm stuck is that I don't know how to write the XPath for the piece I want, because there are multiple nodes with the same name.

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

Don't be hesitant to suggest a better way to do the error handling, as well.

推荐答案

修复xml后...

用xpath遍历这个的方法是:

The way to traverse this with xpath is:

> 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" 

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

The output you can index like normal.

但是,如果节点具有属性,例如 , then you could have used the nice "@" syntax like:

> xpathSApply(plist, '/plist/array/dict/dict/string[@type='uniname']', xmlValue)

另一种可能更适合这种 plist 格式的方法是:

Another way, which might be better for this plist formatting, is:

> sapply(getNodeSet(plist, '//key[text() = "name"]'), function(x) xmlValue(getSibling(x)))
[1] "Queensland University of Technology"

这篇关于有没有更好的方法在 R 中完成这个 XML 抓取任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆