簡體   English   中英

如何使用 R 從 PHP 網站抓取表格?

[英]How can I scrape table from PHP website using R?

希望從此頁面上的表中將數據導入 R:

https://legacy.baseballprospectus.com/standings/index.php?odate=2019-09-10

我嘗試了多種使用 XML 和 httr 的方法,但都沒有成功。 已經看過過去的帖子,包括:

使用 R 從 php 網站讀取數據

使用 XML 包將 html 表抓取到 R 數據框

想知道我是否沒有使用源中正確的表 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 包手冊 ( https://cran.r-project.org/web/packages/XML/XML.pdf ) 中getNodeSetreadHTMLTable的文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM