我想要一些帮助,试图从以下网站得到这一圣经章节的每一节作为一排字符串在一个数据。
我很难找到正确的元素/不知道如何在浏览器中结合使用findElements()和检查元素。对于其他位,例如交叉引用/脚注,任何关于如何一般地这样做的指示都是很棒的.(注意,通过单击页面顶部附近的齿轮,可以通过调整页面选项来查看交叉引用)。
下面是我尝试过的代码。
chapter.url <- "https://www.biblegateway.com/passage/?search=Genesis+50&version=ESV"
library(RSelenium)
RSelenium:::startServer()
remDr <- remoteDriver()
remDr$open()
remDr$navigate(chapter.url)
webElem <- remDr$findElements('id','passage-text')发布于 2014-09-10 09:49:13
通常我会针对相关的HTML。使用firefox firebug或类似的东西检查页面:

相关的HTML片段是<div class="version-ESV result-text-style-normal text-html ">。这样我们就可以找到version-ESV类的元素了
chapter.url <- "https://www.biblegateway.com/passage/?search=Genesis+50&version=ESV"
library(RSelenium)
RSelenium:::startServer()
remDr <- remoteDriver()
remDr$open()
remDr$navigate(chapter.url)
webElem <- remDr$findElement('class', 'version-ESV')
webElem$highlightElement() # check visually we have the right elementhighlightElement方法为我们提供了一个直观的确认,即我们拥有所需的HTML块。最后,我们可以使用getElementAttribute方法获得这个HTML片段:
appData <- webElem$getElementAttribute("outerHTML")[[1]]然后,可以使用XML包解析此HTML中的诗句。
更新:
包含在一个以“ESV”开头的span的id中的各种诗句,我们可以使用'//span[contains(@id,"en-ESV-")]作为XPATH的目标。但是,在这些代码块中,我们只需要作为文本节点的子节点。一旦我们找到这些文本节点,我们希望将它们粘贴到一起,用空格分隔:
appXPATH <- '//span[contains(@id,"en-ESV-")]'
appFunc <- function(x){
appChildren <- xmlChildren(x)
out <- appChildren[names(appChildren) == "text"]
paste(sapply(out, xmlValue), collapse = ' ')
}
doc <- htmlParse(appData, encoding = 'UTF8') # specify encoding
results <- xpathSApply(doc, appXPATH, appFunc)取得了以下结果:
> head(results)
[1] "Then Joseph fell on his father's face and wept over him and kissed him."
[2] "And Joseph commanded his servants the physicians to embalm his father. So the physicians embalmed Israel."
[3] "Forty days were required for it, for that is how many are required for embalming. And the Egyptians wept for him seventy days."
[4] "And when the days of weeping for him were past, Joseph spoke to the household of Pharaoh, saying, “If now I have found favor in your eyes, please speak in the ears of Pharaoh, saying,"
[5] "‘My father made me swear, saying, “I am about to die: in my tomb that I hewed out for myself in the land of Canaan, there shall you bury me.” Now therefore, let me please go up and bury my father. Then I will return.’”"
[6] "And Pharaoh answered, “Go up, and bury your father, as he made you swear.”" https://stackoverflow.com/questions/25761933
复制相似问题