涉及点击的网页刮刮问题(使用R)

时间:2013-04-24 02:16:00

标签: r web-scraping

我正在尝试网络浏览以下网站:

http://www.healthgrades.com/hospital-directory/california-ca-san-mateo/affiliated-physicians-HGSTED418D46050070

我正在使用R来webscrape网站。特别是,我试图从这个网站上复制所有医生的名字和专业。但是,我正在处理的主要问题是,当我按下箭头/下一个按钮时,url链接不会改变。我不能使用任何基本技术来webscrape这个页面。我怎么解决这个问题?将我收集的所有数据放在一个数据矩阵/电子表格中会很好。

2 个答案:

答案 0 :(得分:3)

dum <- "http://www.healthgrades.com/hospital-directory/california-ca-san-mateo/affiliated-physicians-HGSTED418D46050070"
library(XML)
ddum <- htmlParse(dum)
noofpages <- xpathSApply(ddum,'//*/span[@class="paginationItem active"]/following-sibling::*[1]',xmlValue)[1]
noofpages <- (as.numeric(gsub(' of ','',noofpages))-1)%/%5+1
doctors <- c(); dspec <- c()
for(i in 1:noofpages){
 if(i>1){
  ddum <- htmlParse(paste0(dum,"?pagenumber=",i,'#'))
 }
 doctors <- c(doctors, xpathSApply(ddum,'//*/a[@class="providerSearchResultSelectAction"]',xmlValue))
 dspec <- c(dspec, xpathSApply(ddum,'//*/div[@class="listingHeaderLeftColumn"]/p',xmlValue))
}

paste(doctors,dspec,sep=',')
#  [1] "Dr. Julia Adamian, MD,Internal Medicine"                               
#  [2] "Dr. Eric R. Adler, MD,Internal Medicine"                               
#  [3] "Dr. Ramzi S. Alami, MD,General Surgery"                                
#  [4] "Dr. Jason L. Anderson, MD,Internal Medicine"                           
#  [5] "Dr. Karl A. Anderson, MD,Urology"                                      
#  [6] "Dr. Christine E. Angeles, MD,Geriatric Medicine, Pulmonology"          

答案 1 :(得分:2)

看起来他们正在使用变量

?pagenumber=x

您可以迭代x来获取数据。


旁注,

我不确定您使用的浏览器,但Chrome有一个方便的功能,您可以右键单击按钮并选择inspect element