r语言 - 清理论坛帖子,在 rvest + 纵梁中有多个引号



我正在抓取一个很长的论坛线程,我想想出一个数据库,其中包含包含以下信息的列:日期/全文文本/引用用户/引用文本/干净文本

干净的文本应该是每个用户的帖子,如果他们回复任何人,则没有引号。 如果帖子不是回复,我会将其保留为 NA。以下是一篇发明的帖子,带有发明的用户,以说明我到目前为止设法做到的事情:

post<-"Meow1 wrote: »noday is gonna be the day that they're gonna throw it back to you?nBy now you should've somehow Realized what you gotta donnnI don't believe that anybody Feels the way I do, about you nownMeow1 wrote: »nI'm sure you've heard it all before But you never really had a doubtnnnBecause maybe, you're gonna be the one that saves menMeow1 wrote: »nAnd after all, you're my wonderwallnnnAnd all the lights that lead us there are blinding"

然后我尝试拉出引用的用户 (Meow1),它可以工作:

QuotedUser_1<-ifelse(grepl('wrote:', post), gsub('\s*wrote.*$', '', post), NA) 
QuotedUser_1
[1] "Meow1"

然后我创建了这个代码来提取引用的文本和干净的文本:

Quotedtext_1<- ifelse(grepl('wrote:', post), gsub('^.*wrote\s*|\s*\n\n\n.*$', '', post), NA)

当只有一个引用的文本时,它有效,但除此之外,它只给出最后一个引用位(在示例中,"毕竟,你是我的奇迹墙") 对于干净的文本也是如此,它只返回最后一个回复:

Clean_text<- sub('^.*\n\n\n\s*|\s*wrote.*', '', post)

如果有人有改进代码的建议,以便我可以拥有一个包含所有引号的向量和一个包含所有回复的向量,我将不胜感激......

干杯

您确定不能分别抓取作者和文本信息吗?没有来源很难知道,但我想它们可以通过不同的 css 选择器获得,从而更容易拆分数据。 如果没有,研究一下str_locate_all可能会有所帮助,它允许您找到所有出现的,例如"write:"并相应地拆分字符串。

最新更新