![](/img/trans.png)
[英]Webscraping in R, “… does not exist in current working directory” error
[英]Error message: .htm "value" does not exist in current working directory, yet is it part of my webscraping dataframe
我知道这可能是一个带有简单解决方案的幼稚问题。
基本的网络抓取练习,所有内容都在我的 df 中,并且也结构化。 正如你从下面的代码中看到的
library(rvest)
library(dplyr)
library(tidyr)
#load webpage
pres.library <- read_html(x = "http://stateoftheunion.onetwothree.net/texts/index.html")
#get URL of links
links <- pres.library %>%
html_nodes("#text li a") %>%
html_attr("href")
#get link text
text <- pres.library %>%
html_nodes("#text li a") %>%
html_text()
#combine into df, add structure
sotu <- data.frame (text = text, links = links, stringsAsFactors = F) %>%
separate(text, c("President", "Date", "Year"), ",")
#check for unneccesary whitespaces
sotu$President <- trimws(sotu$President)
sotu$links <- trimws(sotu$links)
sotu$Date <- trimws(sotu$Date)
sotu$Year <- trimws(sotu$Year)
#remove data from pre-modern era
sotu.modern <- sotu[-c(1:156),]
#separate democrats from republicans
democrats <- c("Harry S. Truman", "John F. Kennedy", "Lyndon B. Johnson", "Jimmy Carter", "William J. Clinton", "Barack Obama")
sotu.modern$Party <- ifelse(sotu.modern$President %in% democrats, "Democrat", "Republican")
直到这里一切正常。 这里的代码用于将每个链接的文本提取到目录中
for (i in seq(nrow(sotu.modern))) {
sotu.text <- read_html(sotu.modern$links[i]) %>%
html_nodes("#text li a") %>%
html_text()
filename <- paste0("SOTU", President,"-", Date, Year, ".txt")
sink(file = filename) %>%
cat(text) %>%
sink()
}
运行上面的代码给我这个错误信息:
'19460121.html' does not exist in current working directory ('C:/Users/User/Desktop/R/R Projects/SOTU project')
“19460121.html”是 sotu.modern$link 列的第一个元素,所以我不明白为什么它说它不在目录中。
您将不完整的 URL 提供给read_html
。 例如,在第一次迭代中,它指向20200204.html
,而不是完整的 URL http://stateoftheunion.onetwothree.net/texts/20200204.html
我还修复了html_node
和filename
。
for (i in seq(nrow(sotu.modern))) {
sotu.text <- read_html(paste0("http://stateoftheunion.onetwothree.net/texts/",sotu.modern$links[i])) %>%
html_nodes("#text") %>%
html_text()
filename <- paste0("SOTU", sotu.modern$President[i],"-", sotu.modern$Date[i], sotu.modern$Year[i], ".txt")
cat(sotu.text, file=filename)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.