我想从下载的 html 代码中提取信息。html-Code 以字符串形式给出。所需的信息存储在特定的 html 表达式之间。例如,如果我想在字符串中包含每个标题,我必须搜索“H1>”和“/H1>”以及这些 html 表达式之间的文本。
到目前为止,我使用了substr(),但我必须先计算“H1>”和“/H1>”的位置。
htmlcode = " some html code <H1>headline</H1> some other code <H1>headline2</H1> "
startposition = c(21,55) # calculated with gregexpr
stopposition = c(28, 63) # calculated with gregexpr
substr(htmlcode, startposition[1], stopposition[1])
substr(htmlcode, startposition[2], stopposition[2])
输出是正确的,但是要计算每个单独的开始和停止位置是很多工作。相反,我搜索类似 substr () 的函数,您可以在其中使用开始词和停止词而不是位置。例如像这样:function(htmlcode, startword = "H1>", stopword = "/H1>")