2013-08-16 69 views
1

我想從這個網頁訪問的緯度和經度數據 http://chicago.craigslist.org/search/apa?query=pilsen&zoomToPosting=&srchType=A&minAsk=&maxAsk=&bedrooms=&housing_type=Webscraping與R和XML套餐

來源是這樣的:

<p class="row" data-latitude="41.8500405654737" data-longitude="-87.6500521703668" data-pid="4005695169"> <a href="/chc/apa/4005695169.html" class="i"></a> <span class="pl"> <span class="star"></span> <span class="date">Aug 16</span> <a href="/chc/apa/4005695169.html">UIC/ Medical Dist /Pilsen</a> </span> <span class="l2"> <span class="price">$850</span>/1br - <span class="pnr"> <small> (Pilsen)</small> <span class="px"> <span class="p"> <a href="#" class="maptag" data-pid="4005695169">map</a></span></span> </span> </span></p> 

我是新來XPath和我想知道我是否可以使用它和XML包來獲取數據。

我曾嘗試:

require(XML) 
url <- "http://chicago.craigslist.org/search/apaquery=pilsen&zoomToPosting=&srchType=A&minAsk=&max Ask=&bedrooms=&housing_type=" 

doc <- htmlParse(url) 
latitude <- xpathApply(doc,path="//p[@data-latitude]",fun=xmlValue) 

這使我的內容在p標籤:

[[1]] 
[1] "Aug 16 AMAZING Pilsen 2bed SHOWING TODAY @6:30pm $695/2br - 725ft² -  

我想知道如何訪問形成段落標記,如緯度信息(例如41.8500405654737)感謝您的幫助!

回答

4

如果您只是想要特定屬性,請使用xmlGetAttr而不是xmlValuename = 'data-latitude'。如果你想要的所有屬性使用xmlAttrs

latitude <- xpathSApply(doc,path="//p[@data-latitude]",fun = xmlGetAttr , name = 'data-latitude') 
latitude 
[1] "41.963428913124" "41.9515686867654" "41.8634477778791" "41.8500405654737" 
[5] "41.8500405654737" "41.8517021430795" "41.8548534109526" "41.8551971856296" 
[9] "41.8540512700394" "41.8118242805405" "41.8467747060416" "41.8527907628902" 
[13] "41.8615570171552" "41.8500405654737" "41.8514729599615" "41.8514729599615" 
[17] "41.8500405654737" "41.8514729599615" "41.9457245172554" "41.9391355026118" 
[21] "41.8766258071664" "41.8553117771886" "41.940625192879" "41.9457245172554" 
[25] "41.9391355026118" "41.8807511032911" "41.9457245172554" "41.903440231977" 
[29] "41.8780582016541" "41.8950177523891" "41.8541085658189" "41.9391355026118" 
[33] "41.9395365730683" "41.8667136373111" "41.8667136373111" "41.8353155501396" 
[37] "41.9181079515316" "41.903440231977" "41.9208581489481" "41.89490316083" 
[41] "41.903440231977" "41.9017213585917" "41.903440231977" "41.8411597196497" 
[45] "41.8520459177566" "41.8527907628902" "41.8617289044938" "41.8527907628902" 
[49] "41.963428913124" "41.9457245172554" "41.8118242805405" "41.8297005637477" 
[53] "41.903440231977" "41.8762247367098" "41.897710654026" "41.8588641155183" 
[57] "41.8667136373111" "41.8667136373111" "41.9181079515316" "41.903440231977" 
[61] "41.8500405654737" "41.9181079515316" "41.9181079515316" "41.92062896583" 
[65] "41.903440231977" "41.9474433906407" "41.903325640418" "41.8950177523891" 
[69] "41.83176321181" "41.8537074953624" "41.903440231977" "41.9391355026118" 
[73] "41.9457245172554" "41.8569733547944" "41.8402429871775" "41.8950177523891" 
[77] "41.8762247367098" "41.8950177523891" "41.8762247367098" "41.8762247367098" 
[81] "41.8368052404069" 
+0

也在訪問屬性直接在查詢'xpathSApply(doc,「// p [@ data-latitude]/@ data-latitude」,as.character)',甚至是'xpathSApply(doc,「// @ data-latitude」,as.character )' –

2

這是對方的回答略有變異:

latitude <- xpathApply(doc,path="//p[@data-latitude]",fun=xmlAttrs) 

結果(列出的前五個元素):

> latitude 
[[1]] 
       class  data-latitude  data-longitude   data-pid 
       "row" "41.963428913124" "-87.6776687360906"  "3994985952" 

[[2]] 
       class  data-latitude  data-longitude   data-pid 
       "row" "41.9515686867654" "-87.6546931285071"  "3968585242" 

[[3]] 
       class  data-latitude  data-longitude   data-pid 
       "row" "41.8634477778791" "-87.6467290151552"  "3982365094" 

[[4]] 
       class  data-latitude  data-longitude   data-pid 
       "row" "41.8500405654737" "-87.6500521703668"  "4005695169" 

[[5]] 
       class  data-latitude  data-longitude   data-pid 
       "row" "41.8500405654737" "-87.6500521703668"  "4005647462" 
+0

謝謝你們兩位!我在看到其他人之前接受了第一個答案。兩者都適用於我的目的。 – dharv