Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在R中从html文件中读取文本的自定义部分_Regex_R_Rstudio - Fatal编程技术网

Regex 如何在R中从html文件中读取文本的自定义部分

Regex 如何在R中从html文件中读取文本的自定义部分,regex,r,rstudio,Regex,R,Rstudio,我有一些html文本,如下所示。 我想从这篇文章中摘录745部分 我的意思是,对于一个不同的查询,数字可能是其他的(所以我在寻找'of'之后的任何东西) 搜索结果项目:745个中的1到20个 如何在R中使用正则表达式来实现这一点?将正确的HTML解析和正则表达式结合在一起的可重复方式可能是: library(rvest) library(stringr) search_results <- '<div><h2>Search results</h2>&

我有一些html文本,如下所示。 我想从这篇文章中摘录745部分

我的意思是,对于一个不同的查询,数字可能是其他的(所以我在寻找'of'之后的任何东西)

搜索结果项目:745个中的1到20个

如何在R中使用正则表达式来实现这一点?

将正确的HTML解析和正则表达式结合在一起的可重复方式可能是:

library(rvest)
library(stringr)

search_results <- '<div><h2>Search results</h2><h3 class="result_count left">Items: 1 to 20 of 745</h3><span id="result_sel" class="nowrap"></span><input name="EntrezSystem2.PEntrez.Spring.Spring_ResultsPanel.Spring_ResultsController.ResultCount" sid="1" type="hidden" id="resultcount" value="745" /><input name="EntrezSystem2.PEntrez.Spring.Spring_ResultsPanel.Spring_ResultsController.RunLastQuery" sid="1" type="hidden" /></div>'
pg <- read_html(search_results)
items <- html_text(html_nodes(pg, "h3.result_count"))
to_val <- as.numeric(str_match(items, "Items: [[:digit:]]+ to [[:digit:]]+ of ([[:digit:]]+)")[,2])
库(rvest)
图书馆(stringr)

搜索结果首先,HTML结构是否相同(即,这将是具有相同结构的搜索结果的HTML输出)?
library(rvest)
library(stringr)

search_results <- '<div><h2>Search results</h2><h3 class="result_count left">Items: 1 to 20 of 745</h3><span id="result_sel" class="nowrap"></span><input name="EntrezSystem2.PEntrez.Spring.Spring_ResultsPanel.Spring_ResultsController.ResultCount" sid="1" type="hidden" id="resultcount" value="745" /><input name="EntrezSystem2.PEntrez.Spring.Spring_ResultsPanel.Spring_ResultsController.RunLastQuery" sid="1" type="hidden" /></div>'
pg <- read_html(search_results)
items <- html_text(html_nodes(pg, "h3.result_count"))
to_val <- as.numeric(str_match(items, "Items: [[:digit:]]+ to [[:digit:]]+ of ([[:digit:]]+)")[,2])