使用 R 和 XML 包进行网页抓取 [英] Webscraping with R and the XML Package

查看:44
本文介绍了使用 R 和 XML 包进行网页抓取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从此网页访问纬度和经度数据http://chicago.craigslist.org/search/apa?query=pilsen&zoomToPosting=&srchType=A&minAsk=&maxAsk=&bedrooms=&housing_type=

I am trying to access the latitude and longitude data from this webpage 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 的新手,我想知道是否可以使用它和 XML 包来获取数据.

I am new to XPATH and I am wondering if I can use it and the XML package to get the data.

我已经尝试过:

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 标签的内容:

This gives me the content of the of the p tags:

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

我想知道如何访问构成段落标记的信息,例如纬度(例如 41.8500405654737)感谢您的帮助!

I am wondering how to access information that forms the paragraph tag like the latitude (e.g. 41.8500405654737) Thanks for the help!

推荐答案

使用 xmlGetAttr 而不是 xmlValue with name = 'data-latitude'> 如果你只是想要一个特定的属性.如果你想要所有属性使用 xmlAttrs

Use xmlGetAttr rather then xmlValue with name = 'data-latitude' if you just want a specific attribute. If you want all attributes use 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"

这篇关于使用 R 和 XML 包进行网页抓取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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