我正在嘗試將網頁源代碼讀入R并將其作為字符串處理。我正在嘗試刪除段落并從段落文本中刪除html標簽。我遇到了以下問題:
我嘗試實現一個功能來刪除html標簽:
cleanFun=function(fullStr)
{
#find location of tags and citations
tagLoc=cbind(str_locate_all(fullStr,"")[[1]][,1]);
#create storage for tag strings
tagStrings=list()
#extract and store tag strings
for(i in 1:dim(tagLoc)[1])
{
tagStrings[i]=substr(fullStr,tagLoc[i,1],tagLoc[i,2]);
}
#remove tag strings from paragraph
newStr=fullStr
for(i in 1:length(tagStrings))
{
newStr=str_replace_all(newStr,tagStrings[[i]][1],"")
}
return(newStr)
};
這適用于某些標簽,但不適用于所有標簽,此示例失敗的示例是以下字符串:
test="junk junk junk junk"
目標是獲得:
cleanFun(test)="junk junk junk junk"
但是,這似乎不起作用。我認為這可能與字符串長度或轉義字符有關,但是我找不到涉及這些的解決方案。