Adding whitespace to text elements

余生长醉 提交于 2019-12-11 03:54:49

问题


is there a way to add whitespace to each elements that contain text? For this example:

movie <- read_html("http://www.imdb.com/title/tt1490017/") 
cast <- html_nodes(movie, "#titleCast span.itemprop")
cast %>% html_structure()
[[1]]
<span.itemprop [itemprop]>
  {text}

[[2]]
<span.itemprop [itemprop]>
  {text}

I would want to add a trailing whitespace to each text element before using html_text(). I have another use case where I want to use html_text() higher up in the document hierarchy. The result is that several texts get combined within one vector element. This makes it impossible to infer start and end of the corresponding parts.


回答1:


Do you mean something like this?

doc <- minimal_html("Hello<p>World</p>") 
doc %>% html_text # HelloWorld
doc %>% html_text_collapse(" ") # Hello World

If so here is the code:

require(stringi)
require(rvest)

html_text_collapse <- function(x, collapse = " ", trim = TRUE){
  text <- html_text(html_nodes(x, xpath = ".//text()[normalize-space()]"))
  if (trim) {
    text <- stri_trim_both(text)
  }
  paste(text, collapse = collapse)
}


来源:https://stackoverflow.com/questions/42003932/adding-whitespace-to-text-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!