使用R进行网页抓取

使用R进行网页抓取,r,web-scraping,rvest,R,Web Scraping,Rvest,我正试图从中复制医院列表、地址和电话号码 我使用的代码是: # install.packages('rvest') library('rvest') htmlpage <- read_html("http://www.catholichealthinitiatives.org/landing.cfm?xyzpdqabc=0&id=39524&action=list") chihtml <- html_nodes(htmlpage,".info , .address")

我正试图从中复制医院列表、地址和电话号码

我使用的代码是:

# install.packages('rvest')
library('rvest')
htmlpage <- read_html("http://www.catholichealthinitiatives.org/landing.cfm?xyzpdqabc=0&id=39524&action=list")
chihtml <- html_nodes(htmlpage,".info , .address")
chi <- html_text(chihtml)
chi
library(stringr)

chi <- str_replace_all(chi, "[\r\n\t]" , "")
chi
我想删除主线下方的重复地址:

[1] "CHI EX:   St. VincentTwo St. Vincent Cr.Little Rock, AR 72205P 501.552.3000F            501.552.4241"                                 
## remove next line ##
[2] "Two St. Vincent Cr.Little Rock, AR 72205P    501.552.3000F 501.552.4241"

只需在
html\u节点中指定
.info
.address
,具体取决于您想要的:

chihtml <- html_nodes(htmlpage,".info")
chi <- html_text(chihtml, trim = TRUE)    # `trim = TRUE` to strip whitespace
head(chi)
# [1] "CHI St. Vincent\nTwo St. Vincent Cr.Little Rock, AR 72205P 501.552.3000F 501.552.4241"                      
# [2] "CHI St. Vincent Hot Springs\n300 Werner StreetHot Springs National Park, AR 71913P 501.622.1000"            
# [3] "CHI St. Vincent Infirmary\nTwo St. Vincent CircleLittle Rock, AR 72205P 502.552.3000F 501.552.4241"         
# [4] "CHI St. Vincent Morrilton\nFour Hospital DriveMorrilton, AR 72110P 501.977.2300F 501.977.2400"              
# [5] "CHI St. Vincent North\n2215 Wildwood AvenueSherwood, AR 72120P 501.977.2300F 501.977.2400"                  
# [6] "CHI St. Vincent Rehabilitation Hospital\n2201 Wildwood AvenueSherwood, AR 72120P 501.834.1800F 501.834.2227"

chihtml谢谢@alistaire!有没有办法将[1]“CHI St.VincentTwo St.Vincent Cr.Little Rock,AR 72205P 501.552.3000F 501.552.4241”用逗号分开?例如:[1]“CHI St.Vincent,两个圣文森特Cr.,小石城,AR 72205P 501.552.3000,F 501.552.4241”?很抱歉,我对R非常陌生,我正在尝试做一个兼职项目。最好的方法是不要删除
\n
(换行符)字符,它告诉您换行符应该在哪里,所以您可以调用
strsplit(chi,'\n+)
将其拆分为一个列表,每个地址都有一个元素,每个元素都被拆分。如果它很凌乱(
trim=FALSE
),那么strsplit实际上可以完成所有的空白清理:
strsplit(chi,[\n\r\t]+')
,它仍然不会将电话、传真和地址的最后一行分割成额外的一行。为此,您需要一些更严肃的正则表达式。更严肃的正则表达式:
strsplit(trimws(chi),'[\n\r\t]+|(?您帮了我很大的忙。非常感谢。现在是学习此代码并真正理解它的时候了!@alistaire
chihtml <- html_nodes(htmlpage,".info")
chi <- html_text(chihtml, trim = TRUE)    # `trim = TRUE` to strip whitespace
head(chi)
# [1] "CHI St. Vincent\nTwo St. Vincent Cr.Little Rock, AR 72205P 501.552.3000F 501.552.4241"                      
# [2] "CHI St. Vincent Hot Springs\n300 Werner StreetHot Springs National Park, AR 71913P 501.622.1000"            
# [3] "CHI St. Vincent Infirmary\nTwo St. Vincent CircleLittle Rock, AR 72205P 502.552.3000F 501.552.4241"         
# [4] "CHI St. Vincent Morrilton\nFour Hospital DriveMorrilton, AR 72110P 501.977.2300F 501.977.2400"              
# [5] "CHI St. Vincent North\n2215 Wildwood AvenueSherwood, AR 72120P 501.977.2300F 501.977.2400"                  
# [6] "CHI St. Vincent Rehabilitation Hospital\n2201 Wildwood AvenueSherwood, AR 72120P 501.834.1800F 501.834.2227"