【发布时间】:2019-09-23 01:32:45
【问题描述】:
我正在学习数据抓取,除此之外,我还是 R 的新手(在工作中我使用 STATA,我只将 R 用于非常具体的任务)。 为了学习刮痧,我在《今日心理学》上练习了几页。
我编写了一个函数,允许我为一位治疗师抓取信息,并使用以这种方式收集的信息创建一个数据集:
install.packages('rvest') #Loading the rvest package
install.packages('xml2') #Loading the xml2 package
library('rvest') #to scrape
library('xml2') #to handle missing values (it works with html_node, not with html_nodes)
#Specifying the url for desired website to be scraped
url <- 'https://www.psychologytoday.com/us/therapists/THE_ONE_YOU_WANT'
#Reading the HTML code from the website
URL <- read_html(url)
#creating the function
getProfile <- function(profilescrape) {
##NAME
#Using CSS selectors to name
nam_html <- html_node(URL,'.contact-name')
#Converting the name data to text
nam <- html_text(nam_html)
#Let's have a look at the rankings
head(nam)
#Data-Preprocessing: removing '\n' (for the next informations, I will keep \n, to help
# me separate each item within the same type of
# information)
nam<-gsub("\n","",nam)
head(nam)
#Convering each info from text to factor
nam<-as.factor(nam)
#Let's have a look at the name
head(nam)
##MODALITIES
#Using CSS selectors to modality
mod_html <- html_node(URL,'.attributes-modality .copy-small')
#Converting the name data to text
mod <- html_text(mod_html)
#Let's have a look at the rankings
head(mod)
#Convering each info from text to factor
mod<-as.factor(mod)
#Let's have a look at the rankings
head(mod)
##Combining all the lists to form a data frame
onet_df<-data.frame(Name = nam,
Modality = mod)
##Structure of the data frame
str(onet_df)
}
View(onet_df)
无论我选择什么治疗师,这段代码似乎都适用。 现在,我想在多个配置文件上使用此功能,以生成一个数据集,其中包含 MHP 的名称和模态。 假设我想将上述函数“getProfile”应用于伊利诺伊州的前 20 位治疗师,并将这 20 位治疗师的信息输入一个名为“onet_df”的数据集中
j <- 1
MHP_codes <- c(324585 : 449807) #therapist identifier
withinpage_codes <- c(1 : 20) #therapist running number
for(code1 in withinpage_codes) {
for(code2 in MHP_codes) {
URL <- paste0('https://www.psychologytoday.com/us/therapists/illinois/', code2, '?sid=5d87f874630bd&ref=', code1, '&rec_next=1&tr=NextProf')
record_profile <- getProfile <- function(profilescrape)
onet_df[[j]] <- rbind.fill(onet_df, record_profile)
j <- j + 1
}
}
编辑从这里开始:
这个循环不会创建任何数据集;此外,它没有给出任何错误信息。 有人能帮我调试这个循环吗? 请记住,我是一个真正的初学者。
根据建议,我修改了开头的内容:
#creating the function
getProfile <- function(URL) {....}
此外,我使用了三个替代循环:
第一种选择
j <- 1
MHP_codes <- c(324585 : 449807) #therapist identifier
withinpage_codes <- c(1 : 20) #therapist running number
for(code1 in withinpage_codes) {
for(code2 in MHP_codes) {
URL <- paste0('https://www.psychologytoday.com/us/therapists/illinois/', code2, '?sid=5d87f874630bd&ref=', code1, '&rec_next=1&tr=NextProf')
record_profile <- getProfile(URL)
onet_df[[j]] <- rbind.fill(onet_df, record_profile)
j <- j + 1
}
}
给出以下错误消息: UseMethod("xml_find_first") 中的错误: 没有适用于“字符”类对象的“xml_find_first”方法
第二选择
MHP_codes <- c(324585, 449807) #therapist identifier
withinpage_codes <- c(1:20) #therapist running number
df_list <- vector(mode = "list",
length = length(MHP_codes) * length(withinpage_codes))
j <- 1
for(code1 in withinpage_codes) {
for(code2 in MHP_codes) {
URL <- paste0('https://www.psychologytoday.com/us/therapists/illinois/', code2, '?sid=5d87f874630bd&ref=', code1, '&rec_next=1&tr=NextProf')
df_list[[j]] <- getProfile(URL)
j <- j + 1
}
}
final_df <- rbind.fill(df_list)
这个循环给出了同样的错误信息(请参考上面那个)。
现在,我只想弄清楚为什么循环没有生成数据集。可能有两个问题:首先,循环中的某些东西不起作用(我只在一个现有页面上运行了两个循环,并且没有生成数据集); 其次,当我在一系列链接上运行循环时,其中一些可能会丢失,这会产生错误消息。
【问题讨论】:
-
对于一个所谓的真正的初学者很好的问题。希望有更多这样的。 +
-
谢谢!我只是想复制其他人已经完成的类似抓取项目(我有几个参考资料,所以我最终在这里不提了)
标签: r loops web-scraping