【问题标题】:How to split merged/glued words with no delimiter using R如何使用 R 拆分没有分隔符的合并/粘合单词
【发布时间】:2021-01-29 03:53:18
【问题描述】:

我正在使用 R 中的 rvest 使用以下代码从这篇文章页面中抓取文本关键字:

#install.packages("xml2") # required for rvest
library("rvest") # for web scraping
library("dplyr") # for data management

#' start with get the link for the web to be scraped
page <- read_html("https://www.sciencedirect.com/science/article/pii/S1877042810004568")
keyW <- page %>% html_nodes("div.Keywords.u-font-serif") %>% html_text() %>% paste(collapse = ",")

它给了我:

> keyW    
[1] "KeywordsPhysics curriculumTurkish education systemfinnish education systemPISAphysics achievement"

使用这行代码从字符串中删除单词“Keywords”及其之前的任何内容后:

keyW <- gsub(".*Keywords","", keyW)

新的密钥W是:

[1] "Physics curriculumTurkish education systemfinnish education systemPISAphysics achievement"

但是,我想要的输出是这个列表:

[1] "Physics curriculum" "Turkish education system" "finnish education system" "PISA" "physics achievement"

我应该如何解决这个问题?我认为这归结为:

  1. 如何正确地从网站上抓取关键词
  2. 如何正确拆分字符串

谢谢

【问题讨论】:

  • 请使您的示例可重现;缺少加载包的 R 代码:library(xml2)

标签: r text-mining gsub rvest strsplit


【解决方案1】:

如果你使用span标签来提取单词,你会直接得到预期的输出。

library(rvest)
page %>%  html_nodes("div.Keywords span") %>% html_text()

#[1] "Physics curriculum"       "Turkish education system" "finnish education system"
#[4] "PISA"                     "physics achievement"    

【讨论】:

    猜你喜欢
    • 2023-03-14
    • 1970-01-01
    • 2021-05-30
    • 2011-09-11
    • 2015-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    相关资源
    最近更新 更多