我正在努力寻找正确的元素/不知道如何将findElements()与浏览器中的inspect元素结合使用.任何关于如何对其他位进行此 *** 作的指示,例如:交叉引用/脚注会很棒…(注意通过点击页面顶部附近的齿轮调整’页面选项’可以看到交叉引用
以下是我尝试过的代码.
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')解决方法 通常我会针对相关的HTML.使用firefox fireBUG或类似的东西检查页面,我们看到:
相关的HTML代码段是< div class =“version-ESV result-text-style-normal text-HTML”>.
所以我们可以找到类版本-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 element
highlightElement方法为我们提供了视觉确认,即我们拥有所需的HTML块.最后,我们可以使用getElementAttribute方法获取此HTML片段:
appData <- webElem$getElementAttribute("outerHTML")[[1]]
然后可以使用XML包解析此HTML的经文.
更新:
包含在以“en-ESV-”开头的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 enCodingresults <- 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," [5] "‘My father made me swear,“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.”"总结
以上是内存溢出为你收集整理的使用inspect元素的RSelenium和findElements全部内容,希望文章能够帮你解决使用inspect元素的RSelenium和findElements所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)