【发布时间】: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"
我应该如何解决这个问题?我认为这归结为:
- 如何正确地从网站上抓取关键词
- 如何正确拆分字符串
谢谢
【问题讨论】:
-
请使您的示例可重现;缺少加载包的 R 代码:
library(xml2)等
标签: r text-mining gsub rvest strsplit