如何使用 rvest 从基于网络的论坛中抓取消息
how to scrape messages from web based forums with rvest
以示例中的 vbulletin 网站为例。
我希望能够只从线程中抓取文本消息。但是,消息的 css 选择器称为 #post_message_xxx,其中 xxx 是可变 ID 号。
我怎样才能将选择器与 html_nodes 部分匹配,以便我得到所有以 #post_message 开头的选择器,而不管它们如何结束?
或者也许我应该问一个更笼统的问题。如果我希望能够将作者归于消息并跟踪消息顺序,我应该如何抓取页面。
谢谢。
library(rvest)
html <- html("http://www.acme.com/forums/new_rules_28429/")
cast <- html_nodes(html, "#post_message_28429")
cast
> <div id="post_message_28429"> Thanks for posting
> this. </div>
>
> attr(,"class")
[1] "XMLNodeSet"
与其使用 css 选择器,不如使用具有 starts-with()
函数的 xpath 选择器
cast <- html_nodes(html, xpath="//div[starts-with(@id,'post_message')]")
或者您实际上也可以使用 "far less powerful" CSS 选择器同样地做到这一点:
cast <- html_nodes(html, "div[id^='post_message']")
以示例中的 vbulletin 网站为例。 我希望能够只从线程中抓取文本消息。但是,消息的 css 选择器称为 #post_message_xxx,其中 xxx 是可变 ID 号。
我怎样才能将选择器与 html_nodes 部分匹配,以便我得到所有以 #post_message 开头的选择器,而不管它们如何结束?
或者也许我应该问一个更笼统的问题。如果我希望能够将作者归于消息并跟踪消息顺序,我应该如何抓取页面。
谢谢。
library(rvest)
html <- html("http://www.acme.com/forums/new_rules_28429/")
cast <- html_nodes(html, "#post_message_28429")
cast
> <div id="post_message_28429"> Thanks for posting
> this. </div>
>
> attr(,"class")
[1] "XMLNodeSet"
与其使用 css 选择器,不如使用具有 starts-with()
函数的 xpath 选择器
cast <- html_nodes(html, xpath="//div[starts-with(@id,'post_message')]")
或者您实际上也可以使用 "far less powerful" CSS 选择器同样地做到这一点:
cast <- html_nodes(html, "div[id^='post_message']")