1

我正在尝试抓取以下网站:

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

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

4

2 回答 2

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"          
于 2013-04-24T04:36:43.523 回答
2

看起来他们正在使用变量

?pagenumber=x

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


顺便说一句,

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

于 2013-04-24T03:17:18.590 回答