从Gallica XML报纸问题中合并for循环中的文本
我想收集法国报纸Ouest Eclair(1915年)的文字。 Oceized文本可从法国数字图书馆Gallica获得从Gallica XML报纸问题中合并for循环中的文本,r,R,我想收集法国报纸Ouest Eclair(1915年)的文字。 Oceized文本可从法国数字图书馆Gallica获得 library(httr) library(xml2) library(tidyverse) # Newspapers issues identifiers calls arks. They are scrapped from Gallica (XML) and parsed to data frame object r <- GET("https:/
library(httr)
library(xml2)
library(tidyverse)
# Newspapers issues identifiers calls arks. They are scrapped from Gallica (XML) and parsed to data frame object
r <- GET("https://gallica.bnf.fr/services/Issues?ark=ark:/12148/cb41193663x/date&date=1916")
ouest_eclair <- r %>%
content() %>%
xml_find_all(".//issue") %>%
map_df(~ c(as.list(xml_attrs(.x)), date_parution = xml_text(.x)))
# keep only the good colum withs identifiers
arks2 <- ouest_eclair[,'ark']
# The library htm2txt is used to extract easily text from an html page.
library(htm2txt)
# Here's the loop
for (i in arks2) {
url <- paste0("https://gallica.bnf.fr/ark:/12148/", i, ".texteBrut")
print(url)
txt <- gettxt(url)
txt <- paste(txt,txt)
Sys.sleep(1)
}
库(httr)
库(xml2)
图书馆(tidyverse)
#报纸发行识别码。它们从Gallica(XML)中删除,并解析为数据帧对象
r%
xml\u find\u all(“.//问题”)%%>%
map\u df(~c(as.list(xml\u attrs(.x)),date\u parution=xml\u text(.x)))
#只保留带有标识符的好柱
arks2问题在于,在循环的每次迭代中,都会覆盖txt
。诀窍是从循环外部定义的输出的一个(空)变量开始,该变量在每次迭代中都会更新(而不是覆盖),并与另一个确实会被覆盖的临时变量结合使用:
library(htm2txt)
arks2 <- c("bpt6k5674481", "bpt6k567454v", "bpt6k567462f")
txt.output <- "" # start with an empty string of text before you start the loop
for (i in arks2) {
url <- paste0("https://gallica.bnf.fr/ark:/12148/", i, ".texteBrut")
print(url)
txt.temp <- gettxt(url)
txt.output <- paste(txt, txt.temp)
Sys.sleep(1)
}
库(htm2text)
arks2问题在于,在循环的每次迭代中,都会覆盖txt
。诀窍是从循环外部定义的输出的一个(空)变量开始,该变量在每次迭代中都会更新(而不是覆盖),并与另一个确实会被覆盖的临时变量结合使用:
library(htm2txt)
arks2 <- c("bpt6k5674481", "bpt6k567454v", "bpt6k567462f")
txt.output <- "" # start with an empty string of text before you start the loop
for (i in arks2) {
url <- paste0("https://gallica.bnf.fr/ark:/12148/", i, ".texteBrut")
print(url)
txt.temp <- gettxt(url)
txt.output <- paste(txt, txt.temp)
Sys.sleep(1)
}
库(htm2text)
arks2如果您的arks2
是一个数据帧,您可以使用技巧unlist(arks2)
尝试以下操作:
txt如果您的arks2
是一个数据帧,您可以使用技巧unlist(arks2)
尝试以下操作:
txt将它们存储在一个变量中,并使用paste
替代for
循环可以是lappy(c(“BPT6K567441”、“bpt6k567454v”、“bpt6k567462f”)、函数(i)gettext(paste0(“https://gallica.bnf.fr/ark:/12148/,i,“.texteBrut”))
返回返回的文本列表。@sboysel我想保留for循环并将所有文本合并到一个对象中。不是列表。paste(list(“a”,“b”),collapse=“\n”)
将它们存储在一个变量中,并使用paste
替代for
循环可能是lappy(c(“BPT6K567441”,“bpt6k567454v”,“bpt6k567462f”),函数(i)gettext(paste0(“https://gallica.bnf.fr/ark:/12148/,i,“.texteBrut”))
返回返回的文本列表。@sboysel我想保留for循环并将所有文本合并到一个对象中。不是列表。paste(list(“a”,“b”),collapse=“\n”)
它工作得很好,但我的真实数据是一个数据帧列…所以它不工作:文件中的错误(con,“r”)@Wilcar你的意思是arks2
是一个数据帧还是其他什么?你能提供一些数据框作为输入的例子吗?我想这并不难,但我需要知道数据帧在文件(con,“r”)中的错误:无效的“描述”argument@Wilcar查看我的更新解决方案,您可以使用unlist(arks2)
infor(i in arks2)
感谢您的帮助。df有什么问题?它工作得很好,但我的真实数据是一个数据帧列…所以它不工作:文件中的错误(con,“r”)@Wilcar你是说arks2
是数据帧还是其他什么?你能提供一些数据框作为输入的例子吗?我想这并不难,但我需要知道数据帧在文件(con,“r”)中的错误:无效的“描述”argument@Wilcar查看我的更新解决方案,您可以使用unlist(arks2)
infor(i in arks2)
感谢您的帮助。df有什么问题?
txt <- c()
for (i in unlist(arks2)) {
url <- paste0("https://gallica.bnf.fr/ark:/12148/", arks2[k,], ".texteBrut")
print(url)
txt <- c(txt,gettxt(url))
# Sys.sleep(1)
}
txt <- sapply(unlist(arks2), function(v) gettxt(paste0("https://gallica.bnf.fr/ark:/12148/", v, ".texteBrut")))