首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >html_table()只拾取标题行。表有0行。

html_table()只拾取标题行。表有0行。
EN

Stack Overflow用户
提问于 2017-11-30 18:21:38
回答 1查看 1K关注 0票数 1

我正在学习如何使用rvest进行网络搜索,并且遇到了一些问题。具体来说,代码只拾取标题行。

代码语言:javascript
运行
复制
library(rvest)
library(XML)

URL1 <- "https://swishanalytics.com/optimus/nba/daily-fantasy-salary-changes?date=2017-11-25"
df <- URL1 %>% read_html() %>% html_node("#stat-table") %>% html_table()

调用df将生成7列0行的data.frame。我安装了检查器小工具,甚至这也告诉我id = #stat-table是正确的。这个网站的独特之处在于它不想收集表格数据?

作为一个单独的问题,如果我“查看页面源”,我可以看到页面上的所有数据,我不需要使用RSelenium来浏览DK、FD或雅虎的薪水。看起来有些键很容易找到(例如,查找"FD“>查找所有的"player name:”,然后再接字符,等等),但我不知道有一个库/进程处理页面源代码。这有什么资源吗?

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-11-30 19:40:30

您可以--理论上--从<script>标记中提取数据,然后用V8处理它,但这也很容易用splashrseleniumPipes实现。我写了splashr,所以我将向您展示:

代码语言:javascript
运行
复制
library(splashr)
library(rvest)

start_splash()

pg <- render_html(url="https://swishanalytics.com/optimus/nba/daily-fantasy-salary-changes?date=2017-11-25")

html_node(pg, "table#stat-table") %>% 
  html_table() %>% 
  tibble::as_tibble() 
## # A tibble: 256 x 7
##    Position          Player Salary         Change `Proj Fantasy Pts` `Avg Fantasy Pts`   Diff
##       <chr>           <chr>  <chr>          <chr>              <dbl>             <chr>  <chr>
##  1       PF      Thon Maker $3,900  +$600 (18.2%)              12.88             13.24  -0.36
##  2       PG DeAndre Liggins $3,500  +$500 (16.7%)               9.68              7.80  +1.88
##  3       PG   Elfrid Payton $6,400  +$700 (12.3%)              32.77             28.63  +4.14
##  4        C   Jahlil Okafor $3,000 -$400 (-11.8%)               1.71             12.63 -10.92
##  5       PF    John Collins $5,200   +$400 (8.3%)              29.65             24.03  +5.63
##  6       SG     Buddy Hield $4,600  -$400 (-8.0%)              17.96             21.84  -3.88
##  7       SF    Aaron Gordon $7,000   +$500 (7.7%)              32.49             36.91  -4.42
##  8       PG    Kemba Walker $7,600  -$600 (-7.3%)              36.27             38.29  -2.02
##  9       PG    Lou Williams $6,600  -$500 (-7.0%)              34.28             30.09  +4.19
## 10       PG       Raul Neto $3,200   +$200 (6.7%)               6.81             10.57  -3.76
## # ... with 246 more rows

killall_splash()

BeautifulSoup也不会读取这些数据。好吧,您可以将具有JS形式的<script>标记作为目标,并在Python上使用类似的V8引擎,但是它不会比rvest更容易做到这一点。

关于^^的进一步扩展:

大多数刮擦指南告诉您做“检查元素”,以最终找到目标的XPath或CSS选择器。对该表中随机行的检查表明:

对于“正常”网站,这通常是可行的。

带有JS呈现的XHR请求(或页面上的JS+data)的站点看起来像^^,但是如果没有某些渲染引擎的帮助,您的目标将无法工作b/c read_html() (和BeautifulSoup等价物)。您可以尝试通过执行View和元素检查来判断是否正在发生这种情况。下面是该站点的View Source,该源代码裁剪到最后生成表的非常长的数据行+ JS + HTML:

我已经发布了许多关于如何针对这些<script>标记和如何使用V8的答案。使用splashrdecapitated更容易(如果它们已经安装和工作)。

如果您不想与Docker打交道,并且使用最新版本的Chrome,您也可以遵循这里的指导来实现无头工作:

代码语言:javascript
运行
复制
res <- system2("chrome", c("--headless", "--dump-dom", "https://swishanalytics.com/optimus/nba/daily-fantasy-salary-changes?date=2017-11-25"), stdout=TRUE)

然后,res变成了普通的HTML,您可以用rvest读取它并刮掉它。

在开发中的包-- decapitated--使^^不那么难看:

代码语言:javascript
运行
复制
install_github("hrbrmstr/decapitated")
library(decapitated)
library(rvest)

chrome_version()
## Google Chrome 63.0.3239.59 beta

pg <- chrome_read_html("https://swishanalytics.com/optimus/nba/daily-fantasy-salary-changes?date=2017-11-25")

html_node(pg, "table#stat-table") %>% 
  html_table() %>% 
  tibble::as_tibble() 
## # A tibble: 256 x 7
##    Position          Player Salary         Change `Proj Fantasy Pts` `Avg Fantasy Pts`   Diff
##       <chr>           <chr>  <chr>          <chr>              <dbl>             <chr>  <chr>
##  1       PF      Thon Maker $3,900  +$600 (18.2%)              12.88             13.24  -0.36
##  2       PG DeAndre Liggins $3,500  +$500 (16.7%)               9.68              7.80  +1.88
##  3       PG   Elfrid Payton $6,400  +$700 (12.3%)              32.77             28.63  +4.14
##  4        C   Jahlil Okafor $3,000 -$400 (-11.8%)               1.71             12.63 -10.92
##  5       PF    John Collins $5,200   +$400 (8.3%)              29.65             24.03  +5.63
##  6       SG     Buddy Hield $4,600  -$400 (-8.0%)              17.96             21.84  -3.88
##  7       SF    Aaron Gordon $7,000   +$500 (7.7%)              32.49             36.91  -4.42
##  8       PG    Kemba Walker $7,600  -$600 (-7.3%)              36.27             38.29  -2.02
##  9       PG    Lou Williams $6,600  -$500 (-7.0%)              34.28             30.09  +4.19
## 10       PG       Raul Neto $3,200   +$200 (6.7%)               6.81             10.57  -3.76
## # ... with 246 more rows

注意:由于新的权限和沙箱,无头Chrome在High塞拉利昂有问题。它工作在较旧的macOS系统和Windows/Linux上。您只需要正确的版本和正确的环境变量集。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47580162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档