php - 如何使用 R 从 PHP 网站上抓取表格?
问题描述
希望从此页面上的表中将数据导入 R:
https://legacy.baseballprospectus.com/standings/index.php?odate=2019-09-10
我尝试了多种使用 XML 和 httr 的方法,但都没有成功。已经看过过去的帖子,包括:
和
想知道我是否没有使用源中的正确表 ID,或者考虑到我当前使用的工具,表的格式是否不正确?
非常感谢任何和所有帮助!提前致谢!
解决方案
这不会给你你想要的,但它可能会帮助你开始:
library(XML)
fname <- "standings20190910.html"
download.file("https://legacy.baseballprospectus.com/standings/index.php?odate=2019-09-10", destfile=fname)
doc0 <- htmlParse(file=fname, encoding="UTF-8")
doc1 <- xmlRoot(doc0)
doc2 <- getNodeSet(doc1, "//table[@id='content']")
standings <- readHTMLTable(doc2[[1]], header=TRUE, skip.rows=1, stringsAsFactors=FALSE)
您可以查看您尝试抓取的表格的 HTML 源代码,然后尝试找出如何创建有用的 R 对象。仔细查看 XML 包的文档getNodeSet
和readHTMLTable
手册 ( https://cran.r-project.org/web/packages/XML/XML.pdf )。
推荐阅读
- openvas - Greenbone 社区版 (GCE) 不给出结果
- javascript - 如何防止超过 2 个 tokbox 客户端?
- r - 划分 2 行以创建新行
- php - 从一个下拉列表中选择多个项目并插入到多行数据库中
- google-cloud-firestore - Firestore 和自动增量 ID
- arduino - 如何使 .print() 宏默认使用 F() (Arduino)
- python - 从 C++ 运行 .py 脚本
- node.js - 我无法处理 net.createServer 中的错误事件
- react-native - 如何在样式(使用样式组件)视图中显示视图 - React Native
- php - 为什么我应该把省略号放在函数的参数中?