【发布时间】:2016-08-31 13:30:41
【问题描述】:
我正在尝试使用 rvest 包中的 read_html 函数,但遇到了一个我正在努力解决的问题。
例如,如果我试图读取出现在this 页面上的底部表格,我会使用以下代码:
library(rvest)
html_content <- read_html("https://projects.fivethirtyeight.com/2016-election-forecast/washington/#now")
通过在浏览器中检查 HTML 代码,我可以看到我想要的内容包含在 <table> 标记中(具体来说,它都包含在 <table class="t-calc"> 中)。但是当我尝试使用以下方法提取它时:
tables <- html_nodes(html_content, xpath = '//table')
我检索到以下内容:
> tables
{xml_nodeset (4)}
[1] <table class="tippingpointroi unexpanded">\n <tbody>\n <tr data-state="FL" class=" "> ...
[2] <table class="tippingpointroi unexpanded">\n <tbody>\n <tr data-state="NV" class=" "> ...
[3] <table class="scenarios">\n <tbody/>\n <tr data-id="1">\n <td class="description">El ...
[4] <table class="t-desktop t-polls">\n <thead>\n <tr class="th-row">\n <th class="t ...
其中包括页面上的一些表格元素,但不是我感兴趣的。
任何关于我哪里出错的建议将不胜感激!
【问题讨论】:
标签: html r web-scraping rvest