Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R-从各种URL导入和格式化多个表_R_Url_Web Scraping_Import Table - Fatal编程技术网

R-从各种URL导入和格式化多个表

R-从各种URL导入和格式化多个表,r,url,web-scraping,import-table,R,Url,Web Scraping,Import Table,我是R领域的新手,所以下面的问题可能已经有了一些答案,但我还没有找到与我所面临的问题相匹配的解决方案。 我正在尝试从许多网页中获取表格。他们应该在5200左右。 我已经导入了一个,以便格式化它,但我需要自动化的过程,以获得他们所有。 以下是网址: http://www.tbca.net.br/base-dados/int_composicao_estatistica.php?cod_produto=C0195C 我已尝试通过以下操作找到获取所有表格的方法: url <- past

我是R领域的新手,所以下面的问题可能已经有了一些答案,但我还没有找到与我所面临的问题相匹配的解决方案。 我正在尝试从许多网页中获取表格。他们应该在5200左右。 我已经导入了一个,以便格式化它,但我需要自动化的过程,以获得他们所有。 以下是网址:

  http://www.tbca.net.br/base-dados/int_composicao_estatistica.php?cod_produto=C0195C
我已尝试通过以下操作找到获取所有表格的方法:

  url <- paste0("http://www.tbca.net.br/base-dados/int_composicao_estatistica.php?cod_produto=", ., sep="" )
无法读取。 在任何情况下,我都不知道一旦我得到它,如何自动化格式化过程。
有什么提示吗?

以下是一款产品的操作方法:

url <- "http://www.tbca.net.br/base-dados/int_composicao_estatistica.php?cod_produto=C0195C"
h <- read_html(url)
tab <- html_table(h, fill=TRUE) %>% 
  as_tibble(.name_repair = "universal")
tab
# # A tibble: 37 x 1
#    ...1$Componente $Unidades $`Valor por 100… $`Desvio padrão` $`Valor Mínimo` $`Valor Máximo` $`Número de dad…
#    <chr>           <chr>     <chr>            <chr>            <chr>           <chr>           <chr>           
#   1 Energia         kJ        578              -                -               -               -               
#   2 Energia         kcal      136              -                -               -               -               
#   3 Umidade         g         65,5             -                -               -               -               
#   4 Carboidrato to… g         33,3             -                -               -               -               
#   5 Carboidrato di… g         32,5             -                -               -               -               
#   6 Proteína        g         0,60             -                -               -               -               
#   7 Lipídios        g         0,26             -                -               -               -               
#   8 Fibra alimentar g         0,84             -                -               -               -               
#   9 Álcool          g         0,00             -                -               -               -               
#   10 Cinzas          g         0,39             -                -               -               -               
#   # … with 27 more rows, and 2 more variables: $Referências <chr>, $`Tipo de dados` <chr>

编辑 接下来,我们可以跟踪每个链接并将表从中拉出,将表存储在名为
tabs
的列表中。在回答如何在数据中获取产品名称的问题时,有两件事很简单。第一种方法是将表生成一个数据帧,然后在数据帧中生成一个具有代码名的变量(我称之为
code
)。第二种方法是将列表名称设置为产品代码。下面的答案已被编辑以实现这两个目的

all_links <- unique(all_links)
tabs <- vector(mode="list", length=length(all_links))
for(i in 1:length(all_links)){
  url <- glue("http://www.tbca.net.br/base-dados/{all_links[i]}")
  code <- gsub(".*=(.*)$", "\\1", url)
  h <- read_html(url)
  tmp <- html_table(h, fill=TRUE)[[1]]
  tmp <- as.data.frame(tmp)
  tmp$code <- code
  tabs[[i]] <- tmp
  names(tabs)[i] <- code
}


all_links首先,非常感谢您的帮助:该例程运行完美,非常清晰。唯一的问题是,表格上没有标明产品的代码,我不知道每个表格所指的代码。在任何情况下,这都会显示在每个网页中:我应该通过xpath选择器添加它吗?它是否适合其余的例行程序?在此同时,再次感谢。@IlForna我编辑了答案,以包括在结果表中放入产品代码变量的方法,以及使列表命名为产品代码的方法。如果你还有别的想法,请告诉我。这太完美了,非常感谢。实际上,我仍然需要了解最后一段代码是如何工作的,但它确实可以工作。@IlForna主要部分是:
code
library(glue)
all_links <- NULL
links <- "init"
i <- 1
while(length(links) > 0){
  url <- glue("http://www.tbca.net.br/base-dados/composicao_alimentos.php?pagina={i}&atuald=3")
  h <- read_html(url)
  links <- h %>% html_nodes(xpath = "//a[contains(@href,'cod_produto')]") %>% html_attr("href") %>% unique()
  all_links <- c(all_links, links)
  i <- i+1
}
all_links <- unique(all_links)
tabs <- vector(mode="list", length=length(all_links))
for(i in 1:length(all_links)){
  url <- glue("http://www.tbca.net.br/base-dados/{all_links[i]}")
  code <- gsub(".*=(.*)$", "\\1", url)
  h <- read_html(url)
  tmp <- html_table(h, fill=TRUE)[[1]]
  tmp <- as.data.frame(tmp)
  tmp$code <- code
  tabs[[i]] <- tmp
  names(tabs)[i] <- code
}