1

この Web ページhttp://chicago.craigslist.org/search/apa?query=pilsen&zoomToPosting=&srchType=A&minAsk=&maxAsk=&bedrooms=&housing_type=から緯度と経度のデータにアクセスしようとしています。

ソースは次のようになります。

<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 を初めて使用し、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

2 に答える 2

4

特定の属性だけが必要な場合は、 withxmlGetAttrではなく使用します。すべての属性を使用する場合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"
于 2013-08-16T17:57:56.207 に答える
2

これは、他の回答のわずかな変形です。

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

結果 (リストの最初の 5 つの要素):

> 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"
于 2013-08-16T17:58:58.937 に答える