首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RSelenium和findElements与检查元件的使用

RSelenium和findElements与检查元件的使用
EN

Stack Overflow用户
提问于 2014-09-10 09:33:00
回答 1查看 7K关注 0票数 10

我想要一些帮助,试图从以下网站得到这一圣经章节的每一节作为一排字符串在一个数据。

我很难找到正确的元素/不知道如何在浏览器中结合使用findElements()和检查元素。对于其他位,例如交叉引用/脚注,任何关于如何一般地这样做的指示都是很棒的.(注意,通过单击页面顶部附近的齿轮,可以通过调整页面选项来查看交叉引用)。

下面是我尝试过的代码。

代码语言:javascript
复制
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')
EN

Stack Overflow用户

回答已采纳

发布于 2014-09-10 09:49:13

通常我会针对相关的HTML。使用firefox firebug或类似的东西检查页面:

相关的HTML片段是<div class="version-ESV result-text-style-normal text-html ">。这样我们就可以找到version-ESV类的元素了

代码语言:javascript
复制
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 element

highlightElement方法为我们提供了一个直观的确认,即我们拥有所需的HTML块。最后,我们可以使用getElementAttribute方法获得这个HTML片段:

代码语言:javascript
复制
appData <- webElem$getElementAttribute("outerHTML")[[1]]

然后,可以使用XML包解析此HTML中的诗句。

更新:

包含在一个以“ESV”开头的spanid中的各种诗句,我们可以使用'//span[contains(@id,"en-ESV-")]作为XPATH的目标。但是,在这些代码块中,我们只需要作为文本节点的子节点。一旦我们找到这些文本节点,我们希望将它们粘贴到一起,用空格分隔:

代码语言:javascript
复制
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)

取得了以下结果:

代码语言:javascript
复制
> 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.”"                                                                                    
票数 9
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25761933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档