【问题标题】:How to scrape a table into R that requires a button pressed to view the table?如何将表格刮入需要按下按钮才能查看表格的 R 中?
【发布时间】:2020-07-16 00:12:55
【问题描述】:

如何在需要点击标签和按钮的地方抓取表格才能在网站上查看表格。

这些是在https://edudata.fldoe.org/ReportCards/Schools.html?school=0000&district=00查看表格的步骤:

  1. 点击“毕业及以后”
  2. 点击“高等教育继续率”
  3. 点击“创建中学后继续数据表”
  4. 点击“查看数据”

R 版本 3.6.2

我开始使用 rvest_0.3.5

read_html("https://edudata.fldoe.org/ReportCards/Schools.html?school=0000&district=00")
node <- url %>% 
    html_nodes("div.my_container.margin_top_170") %>% 
    html_nodes("div.col-sm-12.col-md-12.col-lg-12.mt-2") %>% 
    html_nodes("div") %>% 
    html_nodes("#accgrad")
node
[1] <div id="accgrad" class="collapse" aria-labelledby="gradhead" data-parent="#primaryaccordion"

我检查了 HTML 以及我想要的 id、类和父匹配,但我无法在我的 R 脚本中进一步定位表格。

我也尝试过使用 xml2_1.2.2

theurl <- getURL("https://edudata.fldoe.org/ReportCards/Schools.html?school=0000&district=00")
tables <- readHTMLTable(theurl)
tables
$`NULL`
   A  B  C  D  F
1               

$`NULL`
   A  B  C  D  F
1               

[[3]]
  TS&I CS&I
1    

我确实注意到其中有一个看起来像是在提取数据的脚本,但我没有足够的 html 知识来确定。

<script> $("#accgrad").load("accordions/acc_graduation.html");</script>

看起来该表位于:

<table id="tablePostsecenroll" class="display no-footer dataTable dtr-inline" 
style="width:100%;" role="grid" aria-describedby="tablePostsecenroll_info">

一旦我可以访问该表,我就会想将它读入 data.frame。但是一旦我可以访问表格的 html 文本,我可能就可以弄清楚。

感谢您花时间解决这个问题!

【问题讨论】:

    标签: html r xml web-scraping rvest


    【解决方案1】:

    数据不在页面源上。它是由 javascript 生成的。所以,rvest不能直接用来刮数据表。

    其中一种方法是使用XHR:查找返回数据的请求 URL(打开 Chrome 或 Firefox 开发人员工具,转到“网络”选项卡,加载您的页面/数据,然后找到适当的请求)。一旦你找到它,让你的爬虫向这个 URL 发送请求。

    【讨论】:

    • 感谢您的回答,这正是我所缺少的。我会赞成这一点,除非我没有足够的声誉这样做。如果其他人看到它,请点赞努兰迪的回答。有关请求 URL 的更多信息,我使用了这篇文章:developers.google.com/web/tools/chrome-devtools/network/…
    • 您应该能够将答案标记为正确,即使您无法投票。
    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 2020-11-05
    • 2013-10-28
    • 1970-01-01
    • 2020-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多