R - 抓取多个 url 并将每个数据 url 写入不同的 excel 数据表
R - Scrape multiple url and wirte each data url in different excel data sheets
我正在尝试抓取不同的 URL 并在同一个 Excel 的文件中写入数据,但每个 URL.
在一个页面中
我的代码是这样的:
#install.packages("rvest")
library(XLConnect)
library(rvest)
{
for(i in c("2086","2167","2204")) {
url<-paste0("https://www.silversanz.com/producto/",i,)
}
dades<-read_html(url)
nom<-dades %>% html_nodes("h1.title") %>% html_text() %>% trimws()
preu<-dades %>% html_nodes("p.price--current") %>% html_text() %>% trimws()
info<-as.data.frame(cbind(nom,preu))
writeWorksheetToFile(file="C:/xxx.xxx.xlsx",
data=info,
sheet= "test",
clearSheets=TRUE
)
}
我有两个问题:
此代码无效 ->
for(i in c("2086","2167","2204")) {
url<-paste0("https://www.silversanz.com/producto/",i,)
不知道怎么写一个sheet每个url
提前致谢:-)
你用错了括号。您编写的 for-loop
遍历数字并将最后一个保存在 url
中。您的 for-loop
应该包含您的所有代码:
library(XLConnect)
library(rvest)
for(i in c("2086","2167","2204")) {
url<-paste0("https://www.silversanz.com/producto/",i)
dades<-read_html(url)
nom<-dades %>% html_nodes("h1.title") %>% html_text() %>% trimws()
preu<-dades %>% html_nodes("p.price--current") %>% html_text() %>% trimws()
info<-as.data.frame(cbind(nom,preu))
writeWorksheetToFile(file="C:/xxx.xxx.xlsx",
data=info,
sheet= i,
clearSheets=TRUE)
}
至于 sheet,现在一切都在循环中,只需使用 i
作为 sheet 名称,以便每个 [=] 有一个 sheet 22=].
我正在尝试抓取不同的 URL 并在同一个 Excel 的文件中写入数据,但每个 URL.
在一个页面中我的代码是这样的:
#install.packages("rvest")
library(XLConnect)
library(rvest)
{
for(i in c("2086","2167","2204")) {
url<-paste0("https://www.silversanz.com/producto/",i,)
}
dades<-read_html(url)
nom<-dades %>% html_nodes("h1.title") %>% html_text() %>% trimws()
preu<-dades %>% html_nodes("p.price--current") %>% html_text() %>% trimws()
info<-as.data.frame(cbind(nom,preu))
writeWorksheetToFile(file="C:/xxx.xxx.xlsx",
data=info,
sheet= "test",
clearSheets=TRUE
)
}
我有两个问题:
此代码无效 ->
for(i in c("2086","2167","2204")) { url<-paste0("https://www.silversanz.com/producto/",i,)
不知道怎么写一个sheet每个url
提前致谢:-)
你用错了括号。您编写的 for-loop
遍历数字并将最后一个保存在 url
中。您的 for-loop
应该包含您的所有代码:
library(XLConnect)
library(rvest)
for(i in c("2086","2167","2204")) {
url<-paste0("https://www.silversanz.com/producto/",i)
dades<-read_html(url)
nom<-dades %>% html_nodes("h1.title") %>% html_text() %>% trimws()
preu<-dades %>% html_nodes("p.price--current") %>% html_text() %>% trimws()
info<-as.data.frame(cbind(nom,preu))
writeWorksheetToFile(file="C:/xxx.xxx.xlsx",
data=info,
sheet= i,
clearSheets=TRUE)
}
至于 sheet,现在一切都在循环中,只需使用 i
作为 sheet 名称,以便每个 [=] 有一个 sheet 22=].